gpt4 book ai didi

ios - NSFetchedResultsController didChangeObject 带有过滤 NSPredicate

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:10 24 4
gpt4 key购买 nike

我有一个由 NSFetchedResultsController 支持的 UITableView,它显示已被用户添加为书签的项目。项目可以从行内的按钮取消书签,这会导致问题。项目取消书签后,它应该从 TableView 中消失,因为它不再与谓词匹配,但因为我的每个部分的行数已被更新更改,我得到了这个错误的变体:

CoreData: error: Serious application error. An exception was caught from the delegate NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

这是我非常简单的 didChangeObject 方法:

-(void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{

[super controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

有什么方法可以指示 NSFetchedResultsController 不处理不匹配的计数吗?还是我需要完全不同的方法?

最佳答案

您的 didChangeObject 委托(delegate)方法看起来很不完整,特别是它不会检查哪个事件发生了(插入、删除或更新)。

您可以在 NSFetchedResultsControllerDelegate 协议(protocol)中找到模板文档。该方法通常类似于以下内容:

- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;

case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}

您还应该实现 controller:didChangeSection:atIndex:forChangeType:
委托(delegate)方法。

我不明白是什么

[super controller:controller didChangeObject:anObject atIndexPath:indexPath forChangeType:type newIndexPath:newIndexPath];

电话是为了!

关于ios - NSFetchedResultsController didChangeObject 带有过滤 NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com