Question

In viewDidAppear method, I initialised an NSTimer. (self.dayView is initialised in the loadView-method).

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(reloadTableView)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)reloadTableView {
    [self.dayView.tableView reloadData];
}

And my cellForRowAtIndexPath looks like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cell for row at indexpath called");
    MatchEventCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    MatchEvent *event = [self getMatchEventAtIndexPath:indexPath];

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
                                                   fromDate:[NSDate date]
                                                     toDate:event.startDate
                                                    options:0];

    cell.textLabel.text = [NSString stringWithFormat:@"%d:%d:%d", [dateComponents hour], [dateComponents minute], [dateComponents second]];
    NSLog(@"TEXT = %@", cell.textLabel.text);

    return cell;
}

As I test this timer in the Simulator, the label in my cell is not updated. When i look at the NSLog, I see that "TEXT = ..." the new value that is set to the textLabel.

Why is my label not visually updated, although the NSLog shows that the textLabel has a new text ?

Était-ce utile?

La solution

Whenever you make changes to the underlying data source, you need to update the table view manual. UITableView's reloadData method is the quick and inefficient way to do it. The correct way is:

NSArray *cells = [myTableView visibleCells];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (UITableViewCell *cell in cells) {
    [indexPaths addObject:[myTableView indexPathForCell:cell]];
}
[myTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
[indexPaths release];

The rest of the rows that are not visible will be updated once you scroll and they appear.

Let us know if this works, otherwise could be a thread problem...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top