在 iOS 中重新加载特定的 UITableView 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7040740/
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:04:36  来源:igfitidea点击:

Reload specific UITableView cell in iOS

iphoneiosuitableview

提问by pa12

I have a UITableView. I want to update the table data based on selection made. Right now I use [mytable reloadData]; I want to know if there is any way where I can just update the particular cell which is selected. Can I modify by using NSIndexPath or others? Any suggestions?

我有一个UITableView. 我想根据所做的选择更新表数据。现在我使用[mytable reloadData]; 我想知道是否有任何方法可以更新所选的特定单元格。我可以使用 NSIndexPath 或其他方式进行修改吗?有什么建议?

Thanks.

谢谢。

回答by CedricSoubrie

For iOS 3.0 and above, you just have to call :

对于 iOS 3.0 及更高版本,您只需调用:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

To reload row 3 of section 2 and row 4 of section 3 for example, you'll have to do this :

例如,要重新加载第 2 节的第 3 行和第 3 节的第 4 行,您必须这样做:

// Build the two index paths
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:3 inSection:2];
NSIndexPath* indexPath2 = [NSIndexPath indexPathForRow:4 inSection:3];
// Add them in an index path array
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, indexPath2, nil];
// Launch reload for the two index path
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];

回答by Hollance

You can also get a reference to the UITableViewCell object and change its labels, etc.

您还可以获取对 UITableViewCell 对象的引用并更改其标签等。

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = @"Hey, I've changed!";

回答by bosmacs

I think you're looking for this method of UITableView:

我认为您正在寻找以下方法UITableView

 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation