gpt4 book ai didi

ios - 基于自定义核心数据 setter 调度 NSNotification

转载 作者:行者123 更新时间:2023-11-29 04:06:44 25 4
gpt4 key购买 nike

我想根据文档目录中是否存在文件来更改UITableViewCell。我觉得这应该是基于通知的,并且应该在对象 isAvailable 属性发生更改时发送通知。

我不想意外地造成线程问题。由于我在主线程上操作核心数据对象,是否可以在我的 Concrete 类上设置自定义 setter 来发布通知?

最好的方法是什么?我应该创建自己的通知,还是应该 Hook 核心数据已经发布的内容?

最佳答案

如果您使用NSFetchedResultsController,这非常简单。该类可以与 UITableView 结合使用,以减少内存开销并提高响应时间。

您可以在 NSFetchedResultsController Class Reference 找到文档和 NSFetchedResultsControllerDelegate Protocol Reference .

此外,您还可以为 NSFetchedResultsController 实现委托(delegate)方法。实现 NSFetchedResultsController 委托(delegate)方法,它允许您监听数据(您注册的 NSManagedObjectContext )中的添加、删除、移动或更新等操作,因此,你的 table 。

关于这个主题的一个非常好的教程是 core-data-tutorial-how-to-use-nsfetchedresultscontroller .在这里您可以找到设置 UITableViewNSFetchedResultsController 及其委托(delegate)的所有元素。

说到这里,关于您的问题,您可以使用此技术在 isAvailable 属性(特定 NSManagedObject 的)时更改 UITableViewCell 的内容) 变化。特别是,您应该实现以下委托(delegate)方法来响应特定的更改(请参阅评论)。

- (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:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeUpdate: // <---- here you will change the content of the cell based on isAvailable property
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;

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

希望对您有所帮助。

关于ios - 基于自定义核心数据 setter 调度 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15073734/

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