繁体   English   中英

更新tableView中的特定单元格而不调用reloadData

[英]Update Specific cells in a tableView without calling reloadData

我有一个应用程序,其中我有一个UITableview自定义单元格和标题。 单元格具有inputView,因此在选择时它们成为第一响应者并允许用户输入数据。 我希望能够在用户更改时动态更新可见的TableViewCell和标头信息..很简单,只需调用[tableview reloadData]; ..不幸的是,这导致inputview辞职第一响应者并隐藏自己。

有什么方法可以在UITableview获取对单元格本身的引用,这样我就可以更改文本属性了吗? cellForRow:atIndexPath:返回一个具有相同属性的新对象,因此不起作用)似乎唯一简单的解决方案可能是每次填充单元格时将单元格中的单元格存储在一个字典中,而不是真正理想的解决方案。

cellForRowAtIndexPath实际上就是这样

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *orderCell;
    static NSString *productCellIdentifier = @"ImageDetailCellIdentifier";
    orderCell = [self.tableView dequeueReusableCellWithIdentifier:productCellIdentifier forIndexPath:indexPath];

//set a bunch of properties orderCell.blah
    return orderCell;
}
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; ///as your choice in animation
[tableView endUpdates];

要不然

 [tableView beginUpdates];
// do some work what ever u need
 [tableView endUpdates];

要重新加载特定的行,您可以使用

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

例如,

NSIndexPath* indexPath = [NSIndexPath indexPathForRow:2 inSection:1];
NSArray* indexArray = [NSArray arrayWithObject:indexPath];

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

根据UITableView文档, -cellForRowAtIndexPath:返回表示表格单元格的对象,如果单元格不可见或indexPath超出范围, -cellForRowAtIndexPath:返回nil

这也是我记得它的方式。 我不认为你的观察是正确的,它返回一个新的对象。 如果单元格可见,您将获得它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM