开发者

Update old/new top/bottom cells' backgroundView when inserting/deleting top/bottom cells

开发者 https://www.devze.com 2023-04-03 02:13 出处:网络
This is the template code given in the doc of NSFetchedResultsControllerDelegate: - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

This is the template code given in the doc of NSFetchedResultsControllerDelegate:

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


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
    atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

  开发者_如何学运维  switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                            withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (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:[tableView cellForRowAtIndexPath:indexPath]
                  atIndexPath:indexPath];
            break;

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


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

With this code, default grouped styled table view updates its cells nicely with background view change animations: when deleting the first cell, the second cell becomes the first with its top corners changing from square to round, and things like that.

However, for cells with custom backgroundView,table view does not do the same background view transitions for us. So we'll see things like this:

Before deleting the first row

Update old/new top/bottom cells' backgroundView when inserting/deleting top/bottom cells

After deleting the first row

Update old/new top/bottom cells' backgroundView when inserting/deleting top/bottom cells

How can I restore the nice animation with proper background view updates?


There is no built in, simple way to manage the cell animations with a custom cell background on a grouped tableview.

You either maintain a list of cells whose background needs redrawing then use [cell.backgroundView setNeedsDisplay]

Or you could consider masking the tableview. So you have one cell background but they are clipped see Round corners on UITableView

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号