UITableViewCell textLabel,在使用GCD时,在滚动或触摸发生之前不会更新 [英] UITableViewCell textLabel, does not update until a scroll, or touch happens, while using GCD

查看:305
本文介绍了UITableViewCell textLabel,在使用GCD时,在滚动或触摸发生之前不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决这个问题吗?

Can someone help me figure this out please?

我的 UITableViewCell textLabel ,直到我滚动,或触摸它。

ViewController 加载,显示正确数量的单元格即可。但内容是空白的。我必须触摸它或滚动才能显示我的textLabel。

The ViewController loads, it shows the right amount of cells. But the content is blank. I have to touch it or scroll to make my textLabel appear.

我在这里做错了吗?

- (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

编辑:

对[cell setNeedsLayout]的调用解决了这个问题。谢谢大家的帮助!

A call to [cell setNeedsLayout] fixes this issue. Thank you everyone for your help!

推荐答案

似乎只是设置单元格的文本还不足以刷新它。您是否尝试在设置文本后设置 [cell setNeedsDisplay] ,看看会发生什么?顺便说一句,既然你已经在使用GCD在后台计算东西,你应该尽量避免在主队列上做任何工作。我会写这段代码更像:

It seems that just setting the text of the cell is not enough for it to be refreshed. Have you tried putting [cell setNeedsDisplay] after setting the text and see what happens? BTW, since you are already using GCD to compute stuff in the background you should try to avoid doing any work at all on the main queue. I would write that piece of code more like:

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

这篇关于UITableViewCell textLabel,在使用GCD时,在滚动或触摸发生之前不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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