有人可以帮我解决这个问题吗?
我的 UITableViewCell textLabel ,在我滚动或触摸之前不会更新。
ViewController 加载,显示正确数量的单元格。但内容是空白的。我必须触摸它或滚动才能显示我的textLabel。
我在这里做错了吗?
- (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;
}
感谢任何帮助
谢谢!
努诺
编辑:
对[cell setNeedsLayout]的调用解决了这个问题。谢谢大家的帮助!
答案 0 :(得分:11)
似乎只是设置单元格的文本不足以刷新它。您是否尝试在设置文本后设置[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];
});
答案 1 :(得分:5)
您似乎正在更新另一个线程(不是主线程)的单元格
重新加载tableview时尝试此操作:
目标-C
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
夫特
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
答案 2 :(得分:2)
我猜GCD使用默认的运行循环模式在主线程中运行你的块。尝试另一种方式:
- (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]];
NSString *time = [data objectForKey:@"Time"];
NSString *totalTime = [data objectForKey:@"Total Time"];
NSString *textLabel = [NSString stringWithFormat:@" %@ %@", time, totalTime];
[cell performSelectorOnMainThread:@selector(setText:) withObject:textLabel waitUntilDone:NO modes:@[NSRunLoopCommonModes]]; //instead of using literals you could do something like this [NSArray arrayWithObject:NSRunLoopCommonModes];
});
return cell;
}
答案 3 :(得分:0)
迅速5
DispatchQueue.main.async() {
self.listView.reloadData()
}