当插入/删除顶部/底部单元格时,更新旧/新顶部/底部单元格的backgroundView

这是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]; } 

使用这段代码,默认的分组样式表视图用背景视图更改animation很好地更新它的单元格:删除第一个单元格时,第二个单元格变成第一个顶angular从正方形变成圆形的东西,等等。

但是,对于具有自定义backgroundView单元格,表视图不会为我们执行相同的背景视图转换。 所以我们会看到这样的事情:

在删除第一行之前 在删除之前

删除第一行后 删除后

我怎样才能恢复适当的背景更新的好animation?

没有内置的,简单的方法来pipe理单元格animation,在分组的tableview上使用自定义的单元格背景。

你要么维护一个单元格的列表,其背景需要重绘,然后使用[cell.backgroundView setNeedsDisplay]

或者你可以考虑屏蔽tableview。 所以你有一个单元格的背景,但是他们被裁剪看UITableView的圆angular

这是迄今为止我find的最好的解决scheme。 请享用!

http://nial.me/2011/01/update-a-uitableviewcells-appearance-dynamically/