gpt4 book ai didi

ios - UITableViewCell 标签没有更新

转载 作者:行者123 更新时间:2023-11-28 21:53:44 24 4
gpt4 key购买 nike

我在这里看到了很多帖子,但似乎没有任何效果。我正在尝试更新 Storyboard中设置的自定义 UITableViewCell 标签。当我在下载过程中收到更新通知时,我正在使用此代码:

NSIndexPath *index = [self.fetchedResultsController indexPathForObject:MyObject];
MyListTableViewCell *cell = (MyListTableViewCell *)[self.tableView cellForRowAtIndexPath:index];
//get the update value then
cell.bottomLabel.text = [NSString stringWithFormat:@"%.1f MB of %.1f MB", megaBytesDownloaded, totalDownloadEstimate];

我可以在日志中看到值是正确的,并且我在更新行上的断点恰好在它应该的时候命中,但是标签只在我与单元格交互时更新,或者由于某种原因,在我与单元格交互后几秒钟下载过程已完成。

我正在使用 AFNetworking,我的所有 IBOutlets 都经过了一次又一次的检查。

如何实时更新单元格?

最佳答案

您正在 tableView 重新加载之外调用 cellForRowAtIndexPath。这将为您提供一个重复使用的单元格或一个全新的实例,但不会为您提供当时屏幕上显示的单元格。

您应该做的是调用 reloadRowsAtIndexPaths:withRowAnimation。

您的代码应该有一个数据源,您可以在当前尝试更新单元格底部标签的位置更新该数据源。

那么你的代码应该是这样的:

self.dataSource.megaBytesDownloaded = megaBytesDownloaded;
self.dataSource.totalDownloadEstimate = totalDownloadEstimate;

NSIndexPath *index = [self.fetchedResultsController indexPathForObject:MyObject];
[self.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];

然后在您的 cellForRowAtIndexPath: 方法中,您可以在此时更新单元格的 bottomLabel。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Your normal setup code here
cell.bottomLabel.text = [NSString stringWithFormat:@"%.1f MB of %.1f MB", self.dataSource.megaBytesDownloaded, self.dataSource.totalDownloadEstimate];
}

由于看起来您也在使用 AFNetworking 来观察下载进度,因此您还应该考虑将 View 更新代码放在主线程上。 AFNetworking 在这种情况下使用多线程来防止 UI 中的任何跳跃或滞后。我建议使用 GCD 以允许主线程处理更新您的 UI 代码。

它应该看起来像这样:

dispatch_async(dispatch_get_main_queue(), ^{
//view update code here.
});

关于ios - UITableViewCell 标签没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com