简体   繁体   中英

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. 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. 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 :

- (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:

- (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. 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! 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.)

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM