Question

Im trying to update the content of my cell based on a timer. When the view loads and timer kicks off. The timer is a countdown timer and each tableview cell text should change based on the timer. The timer is a countdown timer.

-(void)updateLabel{
if(counterValue == 0){
    [self killTimer];
}

    CountdownTableViewMainCell * cell = [self.countdownTableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:0];//i cant get it to work on one cell (the goal is to get it to work on all cells)
    cell.countdownLabel.text = [NSString stringWithFormat:@"%d", counterValue];

   counterValue--;

}

CountdownTableViewMainCell.h

@interface CountdownTableViewMainCell : UITableViewCell

@property (nonatomic,strong) UILabel * countdownLabel;
@property (nonatomic,strong) UILabel * minsLabel;

@end

CountdownTableViewMainCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.countdownLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 21, 21)];
        self.countdownLabel.text = @"14";
        self.countdownLabel.font =[UIFont systemFontOfSize:16.0];
        self.countdownLabel.adjustsFontSizeToFitWidth = YES;

        [self.contentView addSubview:self.countdownLabel];

}

The problem is that the self.countdownLabel does not update. I have logged counterValue and it works as expected.

Was it helpful?

Solution

In UITableViews, you update the cell's contents on tableView:cellForRowAtIndexPath. dequeueReusableCellWithIdentifier: will only get a new cell for you from the cell queue UITableView maintains. It will NOT get the you the currently displayed UITableViewCell.

Source: UITableView docs

Call this method from your data source object when asked to provide a new cell for the table view.

What you should do is maintain a property on your view controller for the counter value.

@property NSInteger counterValue;

Update that property in the method your timer is calling. and then reload your tableView:

self.counterValue--
[self.countdownTableView reloadData]

In your tableView:cellForRowAtIndexPath:, you can do the following:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //Load cell
  ...
  cell.countDownLabel = [NSString stringWithFormat:@"%d", self.counterValue];
}

This should update the countdown label every time it calls the timer method.

OTHER TIPS

Try to use

CountdownTableViewMainCell * cell = [self.countdownTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.countdownLabel.text = ...
[cell setNeedsLayout];

instead of

CountdownTableViewMainCell * cell = [self.countdownTableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:0];

OR

In updateLabel simply call [self.countdownTableView reloadData]; and configure your cells based on counterValue in cellForRowAtIndexPath dataSource method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top