简体   繁体   English

当使用多个部分时,如何动画化tableView单元格的删除/添加,而部分的数量可能会发生变化

[英]How to animate the removal/addition of tableView cells when using multiple sections while the number of sections has the possibility of changing

So I have a UITableView with many sections, each section contains items with a certain letter of the alphabet(Alphabetically sorted). 因此,我有一个包含许多部分的UITableView ,每个部分包含带有特定字母(按字母顺序排序)的项目。 The user has the ability to add more data to this table, so when they add a new item that goes in a section that is not there, a new section must be created. 用户可以向该表中添加更多数据,因此当他们添加不存在于该部分中的新项目时,必须创建一个新部分。 Same problem when deleting, if the last item in a section is deleted the whole section would be removed. 删除时同样的问题,如果删除节中的最后一项,则整个节将被删除。 How would I animate this? 我将如何制作动画?

I have found something similar to this: UITableview reloaddata with animation 我发现了类似的东西: UITableview reloaddata with animation

But that does not help me. 但这对我没有帮助。 It recommends using this method(In swift): 建议使用此方法(快速):

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

This works great if you have the same number of sections before and after the reload, but mine does not. 如果您在重新加载之前和之后具有相同数量的段,则此方法非常有用,而我的则没有。 The number of sections could change causing a crash. 部分的数量可能会更改,从而导致崩溃。

For the purpose you mentioned above, you should not reload data. 出于上述目的,您不应重新加载数据。 For adding the new section you should use, 要添加新部分,您应该使用

- (void)insertSections:(NSIndexSet *)sections
      withRowAnimation:(UITableViewRowAnimation)animation

For deleting the section you should use, 要删除您应使用的部分,

- (void)deleteSections:(NSIndexSet *)sections
      withRowAnimation:(UITableViewRowAnimation)animation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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