iOS - UITableView 正确刷新表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7801320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:55:03  来源:igfitidea点击:

iOS - UITableView refresh table properly?

iphoneiosuitableview

提问by aryaxt

I have lots of data and lots of rows in my tableView. when data changes I want to update my visible cells on the screen, I really don't want to use reloadData because it's an expensive call.

我的 tableView 中有很多数据和很多行。当数据更改时,我想更新屏幕上的可见单元格,我真的不想使用 reloadData,因为这是一个昂贵的调用。

Is it possible to somehow update the visible cells only? I tried calling : beginUpdate& endUpdateon the table, but that doesn't work all the time?

是否可以以某种方式仅更新可见单元格?我试过在桌子上调用 : beginUpdate& endUpdate,但这不是一直有效吗?

Any suggestions?

有什么建议?

回答by Seamus Campbell

You can:

你可以:

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] 
                 withRowAnimation:UITableViewRowAnimationNone];

回答by pableiros

For Swift 3:

对于 Swift 3:

tableView.reloadRows(at: tableView.indexPathsForVisibleRows!, with: .none)

回答by Free Mason

Try calling 'deleteRowsAtIndexPaths', a method of UITableView, right after you delete the model from your data source. For example, if you are deleting a row in editing mode, you could write something like the code below. Note that the first line in the 'if' statement removes the object from the data source, and the second line cleans up the table:

尝试在从数据源中删除模型后立即调用 UITableView 的一个方法“deleteRowsAtIndexPaths”。例如,如果您在编辑模式下删除一行,您可以编写如下代码。请注意,'if' 语句中的第一行从数据源中删除对象,第二行清理表:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ( editingStyle == UITableViewCellEditingStyleDelete ) {

         [[[[[ItemsStore sharedStore] parameterArray] objectAtIndex:indexPath.section]  objectForKey:@"data"] removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}