UITableView一次删除所有行

Mar*_*lch 17 objective-c uitableview uikit

如何一次从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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Leg*_*las 49

做这个.

在您重新加载表之前

[tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

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

[myArray removeAllObjects];
[tableView reloadData];
Run Code Online (Sandbox Code Playgroud)


Cha*_*ine 5

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

TableView.dataSource = nil;
[TableView reloadData];
Run Code Online (Sandbox Code Playgroud)

我试过了,它似乎工作.除了在iOS 8.1中,我有随机崩溃.我的意思是随机的,因为我可以执行10x的代码,它会崩溃1x.所以,我使用了建议的答案:

[myArray removeAllObjects];
[tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

现在我很好