简体   繁体   English

每当对应于该单元格的NSFetchedResultsController中的对象发生更改时,如何使单元格的外观更新?

[英]How do I make what my cell looks like update each time there is a change to the object in NSFetchedResultsController that corresponds to the cell?

I'm using Core Data in my app, and with it NSFetchedResultsController to use it in a table view. 我在我的应用程序中使用Core Data,并在其中使用NSFetchedResultsController在表视图中使用它。 I can do various things to the cell/data, like swiping it left or right to delete or mark the cell as read respectively (this also deletes the data). 我可以对单元格/数据执行各种操作,例如向左或向右滑动以分别删除或标记该单元格为已读(这也会删除数据)。

In cases where I say, mark the cell as read (which also sets the corresponding Core Data object's isRead attribute to YES ), I want the cell to update visually to indicate this. 在我说的情况下,将单元格标记为已读(这还将相应的Core Data对象的isRead属性设置为YES ),我希望该单元格进行可视更新以指示这一点。 The visual update is lightening the cell to look read (like what a read vs an unread email looks like). 视觉更新使单元格看起来更亮(例如已读邮件与未读电子邮件的外观)。

How do I have this happen? 我怎么发生的? (Update the cell when the object that corresponds to it has an attribute change, that is.) (即,当对应于其的对象具有属性更改时更新单元格。)

I tried in this delegate method for NSFetchedResultsController : 我在NSFetchedResultsController此委托方法中尝试过:

- (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:
            [self configureCell:(ArticleCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

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

And NSFetchedResultsChangeUpdate is the case that gets selected, so configureCell: is then called, which looks like this: 然后选择NSFetchedResultsChangeUpdate ,然后调用configureCell: NSFetchedResultsChangeUpdate如下所示:

- (void)configureCell:(ArticleCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Article *article = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.article = article;
}

Which invokes my UITableViewCell subclass, ArticleCell 's custom article property setter. 它调用了我的UITableViewCell子类, ArticleCell的自定义article属性设置器。 In that method I basically start off with: 在这种方法中,我基本上从以下内容开始:

- (void)setArticle:(Article *)article {
    if (_article != article) {

Comparing the new article to be set to the existing one for the cell (so it only updates if there's a change). 将要设置为该单元格的现有文章与新文章进行比较(因此,只有在发生更改的情况下它才会更新)。 However, the code inside this if statement is never called! 但是,此if语句中的代码永远不会被调用! It hops right past and exists the method. 它跳过去并存在该方法。 Within that if statement I have all the custom code to set what the cell looks like based on the properties of that article ( isRead , kind , etc.) 在该if语句中,我具有所有自定义代码,可以根据该文章的属性( isReadkind等)来设置单元格的外观

How should I be handling this? 我应该如何处理呢? I just want that whenever there is an update to the object in that corresponds to that cell, the cell reflects the update. 我只希望每当对应于该单元格的对象有更新时,该单元格就会反映该更新。 Be it a change of read state, or whatever. 改变读取状态,还是其他。

In the if statement you're comparing two references to the same object, so it will always be true when the setter is called from a change of type NSFetchedResultsChangeUpdate . 在if语句中,您正在将两个引用与同一个对象进行比较,因此,当从NSFetchedResultsChangeUpdate类型的更改中调用设置方法时,它将始终为true。

You don't really need the if, just trust the code in the controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: method and always update the cell if the method requests it. 您实际上并不需要if,只需信任controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:方法中的代码,并在该方法请求时始终更新该单元格即可。

It doesn't go inside that if because article object is the same. 因为文章对象是相同的,所以它不会进入内部。 However its properties might be updated. 但是,其属性可能会更新。

You might want to change the if or introduce some other method like -[ArticleCell update] that checks if any of the properties that affect the appearance of the cell are changed. 您可能需要更改if或引入其他方法,例如-[ArticleCell update] ,该方法检查是否会影响单元格外观的任何属性被更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么每次更改单元格对应的对象的值时都会调用我的`configureCell:`方法? (但我不保存它。) - Why is my `configureCell:` method called every time I change the value of the object the cell corresponds to? (But I DON'T save it.) 如何在tableView单元格中进行自定义操作? - How do I make custom action in my tableView cell? 如何为每个单元格添加一个按钮? - How do I add a button to each cell? 像小时应用程序一样跟踪每个单元格上的时间 - Tracking the time on each Cell like Hours App 我应该在我的代码中更改什么以使单元格中的文本字段在关闭 VC 后不为零 - What should I change in my code to make textfield in cell not to be nil after dismiss VC 如何同时更新每个自定义表格视图单元格数据? - How to update each custom tableview cell data at the same time? iOS-如何使表格视图中的每个单元格更大? - iOS- How do I make each cell in a table view bigger? 如何基于步进器值而不是swift 3中单元格的物理选择来进行表格视图的单元格选择? - How do I make my table view's cell selection based on a stepper value, not the physical selection of the cell in swift 3? 如何阻止我的应用为每个单元多次访问数据库? - How do i stop my app from accessing the database numerous times for each cell? 自定义UITableViewCell看起来像标准单元格 - Custom UITableViewCell that looks like the standard cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM