gpt4 book ai didi

ios - 当 didChangeObject 被称为 NSFetchedResultsController 时,UITableView 没有更新

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

我在我的 UITableView 中使用 NSFetchedResultsController。我成功接收到对 - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 的委托(delegate)调用
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
但是我对 UITableView 中的插入/更新/删除行所做的更改都没有实际显示出来。我什至只是尝试在 UITableView 上设置背景颜色以查看是否会显示任何更改,但除非我推送一个新的 View Controller 然后弹出,否则它们不会显示。然后我会看到背景颜色和表格更新。

我对 didChangeObject: 方法的实现实际上只是样板模板:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}

- (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:UITableViewRowAnimationFade];
tableView.backgroundColor = [UIColor redColor];
break;

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

case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
tableView.backgroundColor = [UIColor blueColor];
break;

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

我在导航栏中添加了一个带有 IBAction 的按钮,它只调用 [self.tableView reloadData];,每当我点击它时,所有的插入和更新都会显示在表格中。但是,当更改发生时,它们不会显示。

怎么了?

最佳答案

看起来对 didChangeObject(和其他方法)的委托(delegate)调用没有发生在主线程上,这意味着它们无法更新 UI,但这些更改只是被静静地删除了。

我更新了上面包含的三个方法,以便这些方法的主体都在主线程上分派(dispatch),并且一切都按预期进行。下面是一个示例:

dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView beginUpdates];
});

关于ios - 当 didChangeObject 被称为 NSFetchedResultsController 时,UITableView 没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306595/

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