Ios 基于自定义核心数据设置器发送NSN通知

Ios 基于自定义核心数据设置器发送NSN通知,ios,objective-c,cocoa-touch,cocoa,core-data,Ios,Objective C,Cocoa Touch,Cocoa,Core Data,我想根据文档目录中是否存在文件来更改UITableViewCell。我觉得这应该是基于通知的,并且应该在对象isAvailable属性发生更改时发送通知 我不想意外地造成线程问题。既然我在主线程上操作我的核心数据对象,那么在我的具体类上设置一个自定义setter来发布通知是否可以 最好的方法是什么?我应该创建自己的通知,还是应该连接到核心数据已经发布的内容?如果使用NSFetchedResultsController,这非常简单。此类可与UITableView结合使用,以减少内存开销并提高响应时

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

我不想意外地造成线程问题。既然我在主线程上操作我的核心数据对象,那么在我的具体类上设置一个自定义setter来发布通知是否可以


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

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

您可以在和找到文档

此外,您还可以为
NSFetchedResultsController
实现委托方法。通过实现
NSFetchedResultsController
委托方法,您可以在数据(注册的
NSManagedObjectContext
中)以及表中侦听添加、删除、移动或更新等操作

这是一个很好的教程。在这里,您可以找到用于设置
UITableView
NSFetchedResultsController
及其委托的所有元素

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

-(void)控制器:(NSFetchedResultsController*)控制器didChangeObject:(id)索引路径中的一个对象:(NSIndexPath*)变更类型的indexPath:(NSFetchedResultsChangeType)类型newIndexPath:(NSIndexPath*)newIndexPath{
UITableView*tableView=self.tableView;
开关(类型){
案例NSFetchedResultsChangesInsert:
[tableView InsertRowsatindExpath:[NSArray arrayWithObject:newIndexPath]带RowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]和RowAnimation:UITableViewRowAnimationFade];
打破

case NSFetchedResultsChangeUpdate://Wow,谢谢!这非常有帮助。我将立即开始实施!
- (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;
    }
}