UITableView删除和重新加载单元格 [英] UITableView delete and reload cells
问题描述
[tableView deleteRowsAtIndexPaths:toDelete withRowAnimation:UITableViewRowAnimationAutomatic];
这会为删除操作添加一个好的动画。
但是,删除后,我需要更新所有当前可见的行。调用
[tableView reloadData];
,但是好的动画效果消失了。什么是更好的方法来做到这一点?即动画化删除操作,并更新所有当前可见的行?
谢谢!
我需要这样做的原因是每个单元格都包含一个复选框。我的视图控制器是复选框的委托,每个复选框都有一个NSIndexPath与它相关联。当复选框被切换时,委托被称为告诉它,我们为x索引路径切换。现在,如果一些行被删除,索引路径需要更新。
注意: 如评论中所述,很可能此答案无效。如果不接受,我会将其删除。
我不是100%确定这是最好的方式做到这一点,但有一些令人愉快的清洁。在修改底层模型之后,我们可以在常规UIView动画块中封装 reloadData
:
// if self.people是一个可变数组
[self.people removeObjectAtIndex:13];
[UIView animateWithDuration:0.5 animations:^ {
[self.tableView reloadData];
}];
I am delete some rows in UITableView like this:
[tableView deleteRowsAtIndexPaths:toDelete withRowAnimation:UITableViewRowAnimationAutomatic];
This adds a nice animation to the delete operation.
However, after deleting I need to update all the currently visible rows. Calling
[tableView reloadData];
right after the first call works, but the nice animation effect is gone. What's a better way to do this? i.e., to animate the delete operation, and update all the currently visible rows? Thanks!
The reason why I need to do this is because each cell contains a 'checkbox'. My view controller is checkbox's delegate and each checkbox has an NSIndexPath associated with it. When the checkbox is toggled, delegate is called telling it hey we toggled for x index path. Now, if some rows are deleted, the index paths need to be update. That's why I need to reload everything so each checkbox knows where it belongs.
Note: As noted in comments, it is likely that this answer will not work. If it were not accepted, I would delete it.
I'm not 100% sure that this is the best way to do this, but there is something pleasantly clean about it. We can wrap reloadData
in a regular UIView animation block after modifying the underlying model:
// if self.people is a mutable array
[self.people removeObjectAtIndex:13];
[UIView animateWithDuration:0.5 animations:^{
[self.tableView reloadData];
}];
这篇关于UITableView删除和重新加载单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!