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

时间:2014-05-21 05:37:50

标签: ios objective-c uitableview

我有一个应用程序,其中有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;
}

3 个答案:

答案 0 :(得分:1)

[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];

答案 1 :(得分:1)

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

- (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];

答案 2 :(得分:1)

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

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

相关问题