有人可以帮我解决这个问题吗?
My UITableViewCell 文本标签,直到我才更新scroll, or touch it.
The 视图控制器加载,它显示适量的细胞。但内容却是一片空白。我必须触摸它或滚动才能显示我的文本标签。
我在这里做错了什么吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setFont: [UIFont systemFontOfSize: 32.0]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSDictionary * data = [self timeForObject: [self.months objectAtIndex:indexPath.row]];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *time = [data objectForKey:@"Time"];
NSString *totalTime = [data objectForKey:@"Total Time"];
NSString * textLabel = [NSString stringWithFormat:@" %@ %@",
time, totalTime];
[[cell textLabel] setText:textLabel];
});
});
return cell;
}
任何帮助表示赞赏
谢谢你!
Nuno
EDIT:
调用 [cell setNeedsLayout] 修复了此问题。感谢大家的帮助!
看来仅仅设置单元格的文本不足以刷新它。你有没有尝试过把[cell setNeedsDisplay]
设置文本后看看会发生什么?顺便说一句,由于您已经在后台使用 GCD 来计算内容,因此您应该尽量避免在主队列上执行任何工作。我会把那段代码写得更像:
NSDictionary *data = [self timeForObject: [self.months objectAtIndex:indexPath.row]];
NSString *time = [data objectForKey:@"Time"];
NSString *totalTime = [data objectForKey:@"Total Time"];
NSString *textLabel = [NSString stringWithFormat:@" %@ %@", time, totalTime];
dispatch_async(dispatch_get_main_queue(), ^{
[[cell textLabel] setText:textLabel];
[cell setNeedsDisplay];
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)