【问题标题】:UITableView delete all rows at onceUITableView 一次删除所有行
【发布时间】:2011-09-12 20:07:56
【问题描述】:

如何一次从 UITableView 中删除所有行? 因为当我用新数据重新加载表视图时,我仍在插入新行:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    }

    //... setting the new cell here ...

    return cell;
}

谢谢。

【问题讨论】:

  • 从数组中删除所有对象。更新 tableView,然后添加新数据。

标签: objective-c uitableview uikit


【解决方案1】:

这样做。

在重新加载表格之前

[tableView reloadData];

从 tableView 数组中删除所有对象(填充 tableView 的数组) 例如。

[myArray removeAllObjects];
[tableView reloadData];

【讨论】:

  • 删除对象后重新加载表是关键!谢谢
【解决方案2】:

仅供参考,上面的解决方案“删除所有数据源,然后调用 reloadData。”听起来他们在提倡这个:

TableView.dataSource = nil;
[TableView reloadData];

我试过了,它似乎有效。除了,在 iOS 8.1 中,我遇到了随机崩溃。我的意思是随机的,例如,我可以执行该代码 10 倍,它会崩溃 1 倍。所以,我使用了推荐的答案:

[myArray removeAllObjects];
[tableView reloadData];

现在我很好了。

【讨论】:

    【解决方案3】:

    删除你所有的dataSource,然后拨打reloadData

    【讨论】:

      【解决方案4】:

      听起来您在添加更多数据之前没有清除您的数据集合 (NSArray)。 UITableView 通常会显示您的所有数组数据。如果您不想要以前的行/数据,那么您应该在向其添加新信息之前清空您的数组。然后拨打[yourTableView reload]

      【讨论】:

        【解决方案5】:

        先删除你的旧数据

        myArray = nil;
        [tableView reloadData];
        

        然后加载您的新数据

        myArray = newData;
        [tableView reloadData];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-18
          • 1970-01-01
          • 1970-01-01
          • 2017-09-30
          • 2014-03-23
          相关资源
          最近更新 更多