[英]Update Specific cells in a tableView without calling reloadData
I have an app in which I have a UITableview
with custom cells and headers. 我有一个应用程序,其中我有一个
UITableview
自定义单元格和标题。 The cells have an inputView so when selected they become first responder and allow the user to input data. 单元格具有inputView,因此在选择时它们成为第一响应者并允许用户输入数据。 I want to be able to update the visible
TableViewCell
and header information on the fly while the user is changing it.. easy, just call [tableview reloadData];
我希望能够在用户更改时动态更新可见的
TableViewCell
和标头信息..很简单,只需调用[tableview reloadData];
.. Unfortunately this causes the inputview to resign first responder and hide itself. ..不幸的是,这导致inputview辞职第一响应者并隐藏自己。
Is there any way that I can get a reference to the cell itself inside the UITableview
so that I can just change the text property? 有什么方法可以在
UITableview
获取对单元格本身的引用,这样我就可以更改文本属性了吗? ( cellForRow:atIndexPath:
returns a new object with the same properties so doesn't work) It seems like the only easy solution may be to store a reference the cells in a dictionary each time a cell is populated, not really the ideal solution. (
cellForRow:atIndexPath:
返回一个具有相同属性的新对象,因此不起作用)似乎唯一简单的解决方案可能是每次填充单元格时将单元格中的单元格存储在一个字典中,而不是真正理想的解决方案。
cellForRowAtIndexPath is literally just 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];
or else 要不然
[tableView beginUpdates];
// do some work what ever u need
[tableView endUpdates];
For reloading specific rows, you can use 要重新加载特定的行,您可以使用
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
For example, 例如,
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:2 inSection:1];
NSArray* indexArray = [NSArray arrayWithObject:indexPath];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
According to UITableView
documentation, -cellForRowAtIndexPath:
returns an object representing a cell of the table or nil
if the cell is not visible or indexPath is out of range. 根据
UITableView
文档, -cellForRowAtIndexPath:
返回表示表格单元格的对象,如果单元格不可见或indexPath超出范围, -cellForRowAtIndexPath:
返回nil
。
That is also how I remember it. 这也是我记得它的方式。 I don't think your observation is correct that it returns a new object.
我不认为你的观察是正确的,它返回一个新的对象。 If the cell is visible you will get hold of it.
如果单元格可见,您将获得它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.