簡體   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