多个 NSFetchedResultsController - didChangeObject
我有一个 UITableView,它使用 2 个 NSFetchedResultsControllers。每个 NSFetchedResultsController 只有一个部分。但是,该表有 4 个部分。我使用 NSFetchedResultsControllers 之一的结果填充表的第四部分。到目前为止一切正常。但是,如果用户删除第一部分中的第一个单元格,则 NSFetchedResultsControllers 会发生更改。表最后一部分中的行可能会被删除。调用此方法时:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
NSLog(@"section: %d, row: %d", [newIndexPath section], [newIndexPath row]);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
...
}
该部分始终为 0,因为它是 NSFetchedResultsControllers 的部分。因此,该部分与表视图上的正确部分不匹配。
有解决方法吗?我基本上想将 NSFetchedResultsController
的部分更改为 3 而不是 0。
I have a UITableView
which uses 2 NSFetchedResultsControllers
. Each NSFetchedResultsController
has only one section. However, the table has 4 sections. I populate the 4th section of the table with the results of one of the NSFetchedResultsControllers
. Everything works fine so far. But if the user deletes the first cell on the first section, the NSFetchedResultsControllers
are changed. The rows in the last section of the table might be deleted. When this method is called:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
NSLog(@"section: %d, row: %d", [newIndexPath section], [newIndexPath row]);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
...
}
The section is always 0 because it's the section of the NSFetchedResultsControllers
. Thus, the section doesn't match the correct one on the table view.
Is there a workaround? I would basically like to change the section of a NSFetchedResultsController
to 3 instead of 0.
我找到了一个解决方法,但如果有一个更漂亮的解决方案那就太好了。
I found a workaround, but it would be nice to have a prettier solution.