20

Can someone help me figure this out please?

My UITableViewCell textLabel, does not update until I scroll, or touch it.

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.

Am I doing something wrong here?

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

Any help is appreciated

Thank you!

Nuno

EDIT:

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

4

4 回答 4

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];
});
于 2012-08-27T10:44:03.183 回答
7

您似乎正在另一个线程(不是主线程)上更新单元格

重新加载 tableview 时试试这个:

Objective-C

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});

迅速

dispatch_async(dispatch_get_main_queue()) {
    self.tableView.reloadData()
}
于 2015-12-11T03:30:34.010 回答
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;
}
于 2012-08-27T11:49:46.260 回答
1

在迅速 5

    DispatchQueue.main.async() {
        self.listView.reloadData()
    }
于 2019-05-11T01:06:02.853 回答