简体   繁体   English

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

[英]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.

相关问题 UITableView强制sectionIndex更新,而无需调用reloadData - UITableView force sectionIndex to update without calling reloadData reloadData 仅在 tableView 可见单元格上 - reloadData only on tableView visible cells 使用 collectionView(_:layout:sizeForItemAt:) 调整 UICollectionView 单元格而不调用 reloadData() - Resizing UICollectionView cells with collectionView(_:layout:sizeForItemAt:) without calling reloadData() 调用[tableView reloadData]时,TableView无法重新加载 - TableView not reloading when calling [tableView reloadData] TableView不会使用ReloadData()更新 - TableView doesn't update with ReloadData() tableView reloadData不更新数据 - tableView reloadData does not update the data 如何在不调用reloadData的情况下删除UITableView中的行并更新indexPath? - How to delete row in UITableView and update indexPaths without calling reloadData? 调用referenceSizeForFooterInSection而不调用reloadData - calling referenceSizeForFooterInSection without calling reloadData 循环中的tableView reloadData不在每次通过时都绘制单元格 - tableView reloadData in loop not drawing cells on each pass 调用tableView reloadData时出错:更新后现有部分中包含的行数 - Getting an error when calling tableView reloadData: The number of rows contained in an existing section after the update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM