UITableViewCell 标签未更新 [英] UITableViewCell label is not updating

查看:23
本文介绍了UITableViewCell 标签未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have seen many posts on here but nothing seems to be working. I am trying to update a custom UITableViewCell label that is setup in storyboard. I am using this code when I receive an update notification during the download:

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];

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

I can see in logs the values are correct, and my breakpoint on the update line is hit exactly when it should be, but the label only updates when I interact with the cell, or for some reason,a few seconds after my download process has completed.

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

I am using AFNetworking and all of my IBOutlets have been checked again and again.

如何实时更新单元格?

推荐答案

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

You are calling cellForRowAtIndexPath outside of a tableView reload. This will either give you a reused cell or a brand new instance but not the cell shown on the screen at the time.

你应该做的是调用 reloadRowsAtIndexPaths:withRowAnimation.

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

Your code should have a dataSource that you update where you are currently trying to update the cell's bottomLabel.

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

Then your code should look something like this:

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

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

然后在您的 cellForRowAtIndexPath: 方法中,您可以在此时更新单元格的底部标签.

And then in your cellForRowAtIndexPath: method, you can update your cell's bottomLabel at that point.

- (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 来观察下载进度,因此您还应该考虑在主线程上抛出您的视图更新代码.在这种情况下,AFNetworking 使用多线程来防止 UI 中的任何跳跃或滞后.我建议使用 GCD 来允许主线程处理更新您的 UI 代码.

Since it looks like you are also using AFNetworking to observe the download progress, you should also look into throwing your view update code on the main thread. AFNetworking uses multithreading in this case to prevent any jumps or lags in your UI. I would suggest using GCD to allow the main thread to handle updating your UI code.

它应该看起来像这样:

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

这篇关于UITableViewCell 标签未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆