簡體   English   中英

iOS UITableView reloadRowsAtIndexPaths

[英]iOS UITableView reloadRowsAtIndexPaths

我有一個UITableview,懶惰加載所有不同大小的圖像。 加載圖像時,我需要更新特定的單元格,所以我想出了我需要使用reloadRowsAtIndexPaths。 但是當我使用這個方法時,它仍然為每個單元格調用heightForRowAtIndexPath方法。 我認為reloadRowsAtIndexPaths的全部目的是它只會為你指定的特定行調用heightForRowAtIndexPath?

知道為什么嗎?

[self.messageTableView beginUpdates];
[self.messageTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:count inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.messageTableView endUpdates];

謝謝

endUpdates觸發內容大小重新計算,這需要heightForRowAtIndexPath 這就是它的工作原理。

如果這是一個問題,您可以將您的單元配置邏輯拉出cellForRowAtIndexPath並直接重新配置單元格,而無需通過reloadRowsAtIndexPaths 以下是這可能是什么的基本大綱:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = ...;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    [self tableView:tableView configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    //cell configuration logic here
}

然后,無論您當前正在調用reloadRowsAtIndexPaths ,都會執行此操作,並且不會調用heightForRowAtIndexPath

UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
[self tableView:self.messageTableView configureCell:cell atIndexPath:indexPath];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM