插入/删除顶部/底部单元格时更新旧/新顶部/底部单元格的背景视图 [英] Update old/new top/bottom cells' backgroundView when inserting/deleting top/bottom cells
问题描述
这是NSFetchedResultsControllerDelegate
文档中给出的模板代码:
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.
然而,对于具有自定义backgroundView
的单元格,表格视图不会为我们做相同的背景视图转换.所以我们会看到这样的事情:
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:
删除第一行前
删除第一行后
如何通过适当的背景视图更新恢复漂亮的动画?
How can I restore the nice animation with proper background view updates?
推荐答案
没有内置的简单方法可以在分组的 tableview 上使用自定义单元格背景来管理单元格动画.
There is no built in, simple way to manage the cell animations with a custom cell background on a grouped tableview.
您要么维护一个背景需要重绘的单元格列表,然后使用 [cell.backgroundView setNeedsDisplay]
You either maintain a list of cells whose background needs redrawing then use [cell.backgroundView setNeedsDisplay]
或者你可以考虑屏蔽 tableview.所以你有一个单元格背景,但它们被剪裁见 UITableView 上的圆角
Or you could consider masking the tableview. So you have one cell background but they are clipped see Round corners on UITableView
这篇关于插入/删除顶部/底部单元格时更新旧/新顶部/底部单元格的背景视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!