【问题标题】:iOS - UITableView Delete All Rows Within A SectioniOS - UITableView 删除部分内的所有行
【发布时间】: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


【解决方案1】:

如果您想要动画更改,您将需要首先更新您的数据源(返回 0 表示节中的行数)然后删除节并在 one transaction between [tv beginUpdates] [tv endUpdates] 中的同一索引路径添加节

否则只需更新数据源并在返回 VC 的途中重新加载表(如果您不想要任何动画)

【讨论】:

    【解决方案2】:

    我没有详细阅读此内容,但 for 循环肯定应该从零开始。

        for (int i=0; i<rows; i++) {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:section];
            [tmpArray addObject:tmpIndexPath];
        }
    

    否则,您只会删除该部分中除第一个单元格之外的所有单元格。

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      相关资源
      最近更新 更多