【发布时间】:2017-03-30 16:21:00
【问题描述】:
我有一个带有可扩展部分的UITableView。当用户转到另一个视图时,我需要折叠所有展开的部分,我需要将其放入 viewWillDisappear 方法中。
我只找到了有关如何一次从表视图中删除所有行的解决方案,但有没有办法从特定部分删除所有行?
编辑:
我已经找到了一个解决方案,但我不确定它是否是最佳的,或者会导致未来效率低下。每当一个单元格展开时,它就会被添加到NSMutableIndexSet。所以在我的viewWillDisappear 方法中,我像这样遍历展开的部分:
-(void)viewWillDisappear:(BOOL)animated
{
if (expandedSections.count != 0) {
NSLog(@"COLLAPSING CALLED");
[self.tableView beginUpdates];
NSUInteger section = [expandedSections firstIndex];
do
{
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
rows = [self tableView:self.tableView numberOfRowsInSection:section];
[expandedSections removeIndex:section];
for (int i=1; i<rows; i++) {
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
[tmpArray addObject:tmpIndexPath];
}
[self.tableView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
NSIndexPath *expandableCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:expandableCellIndexPath];
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[self.colorHolder objectAtIndex:section] type:DTCustomColoredAccessoryTypeRight];
section = [expandedSections indexGreaterThanIndex:section];
} while (section != NSNotFound);
[self.tableView endUpdates];
}
}
请告诉我这是否是一个好的解决方案,或者,如果我猜对了,当每个展开部分中有更多行时,这是否会导致未来视图之间的转换变慢。任何帮助或建议将不胜感激。谢谢。
【问题讨论】:
-
deleteRowsAtIndexPaths:withRowAnimation:方法有什么问题?只需传入该部分中行的所有索引路径。 -
@rmaddy 我想出了一个解决方案,但我不确定它是否会稳定,因为每个可扩展部分中有更多行。
-
我编辑了我的问题以显示我的解决方案。我不喜欢这个解决方案正在执行嵌套迭代的想法,所以任何反馈都将不胜感激。
标签: ios objective-c uitableview