I have a table view backed up by a NSFetchedResutlsController. I want to support reordering on the table view. I use a property called 'index' in my NSManagedObject entity to record the order for each object, and recalculate the index whenever a reorder action is performed by the user in tableView:moveRowAtIndexPath:toIndexPath: (shown below, the Person is a subclass of NSManagedObject)
- (void) tableView:(UITableView *)tab开发者_如何学编程leView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSMutableArray *userList = [self.personResult.fetchedObjects mutableCopy];
Person *user = [[userList objectAtIndex:fromIndexPath.row] retain];
[userList removeObjectAtIndex:fromIndexPath.row];
[userList insertObject:user atIndex:toIndexPath.row];
[user release];
int indexCounter = 0;
for (Person *user in userList)
{
user.index = [NSNumber numberWithInt:indexCounter++];
}
}
What makes me confused is, if I don't set a delegate for the NSFetchedResutlsController, the order of the objects in the NSFetchedResutlsController will not be updated. So when I click on the cell to view detail information, it will display contents according to the previous order. However, when I set a delegate with empty implementation for the NSFetchedResutlsController (as shown below), the order of the objects in the NSFetchedResutlsController do get updated.
- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller
{
}
- (void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
}
- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller
{
}
I checked the initial value of the delegate property and it is set to nil. So I wonder if the implementation of the framework (maybe Core Data or something) will be different according to the existence of the delegate.
Any idea on this problem?
精彩评论