the_pantless_coder
the_pantless_coder

Reputation: 2297

UITableViewCell countdown UILabel not updating

I am trying to learn more about timers, so I decided to try to create a Todo list type IOS app. In the app I want to allow the user to be able to add a timer to each task/item if they wish.

So far, The user can select a time(from picker), start and stop the timer without any problems. The problem I am running into is that the UILabel will not update and show the countdown timer text. The only time it updates the time is when I go to a subview(edit task) and then return back to the main viewController(task List).

The only way I could get the cell to update was to run a loop in the parent viewController with a [self.tableView reloadData]; I am pretty sure this is like killing a fly with a sledgehammer way of doing it.

Any insight would be great. Thanks.

EDIT:

@property (strong, nonatomic) IBOutlet UILabel *taskNameTextLabel; //Textfield for item
@property (strong, nonatomic) IBOutlet UILabel *taskSubtitleTextLabel; //Textfield to timer

I created a sublass for the tableview cell. here is code I use for the timer in my taskTableViewCell.m file.

-(void) stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

- (void) startTimer
{
    [self stopTimer];
    NSLog(@"time : %f", _taskCountDownInterval);

    [self calculateTimer];
    _timer = [NSTimer timerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(countDownTimerHandler)
                                           userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)calculateTimer
{
    //calculate total seconds from the saved values the user selected using the picker.
    int hours = [self.startHours integerValue];
    int minutes = [self.startMinutes integerValue];
    int seconds = [self.startSeconds integerValue];
    NSLog(@"h: %i m: %i s: %i", hours,minutes,seconds);

    //convert to seconds and get total sum
    hours = hours *3600;
    minutes = minutes *60;
    int totalSeconds = hours + minutes + seconds;
    _taskCountDownInterval = totalSeconds;
}


- ( void) countDownTimerHandler
{
    int remainingTime = _taskCountDownInterval--;
    if (remainingTime <=0)
    {
        [_countDownTimer invalidate];
        _countDownTimer = nil;
        remainingTime = 0;
        NSLog(@"Timer finished!");
    }
    //convert for text display.
   int hours = remainingTime / 3600;
   int scratch = remainingTime % 3600;
   int minutes = scratch / 60;
   int seconds = scratch % 60;

    NSString *timeValue = [NSString stringWithFormat:@"%i:%i:%i",hours,minutes,seconds];
    self.subtitle = timeValue; //save the converted string.

    NSLog(@"label should be  %@",timeValue);
    NSLog(@"label is  %@",self.taskSubtitleTextLabel.text);
    //[[self taskSubtitleTextLabel] setText:_subtitle];
    _taskSubtitleTextLabel.text = _subtitle;
}

the log output is:

2014-06-18 11:52:22.614 mytools2[1438:60b] turn timer ON
2014-06-18 11:52:22.614 mytools2[1438:60b] total time in seconds : 21966.000000
2014-06-18 11:52:22.615 mytools2[1438:60b] h: 6 m: 6 s: 6
2014-06-18 11:52:22.615 mytools2[1438:60b] taskCountDownInterval 21966.000000
2014-06-18 11:52:23.616 mytools2[1438:60b] label should be  6:6:6
2014-06-18 11:52:23.617 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:24.616 mytools2[1438:60b] label should be  6:6:5
2014-06-18 11:52:24.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:25.616 mytools2[1438:60b] label should be  6:6:4
2014-06-18 11:52:25.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:26.616 mytools2[1438:60b] label should be  6:6:3
2014-06-18 11:52:26.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:27.616 mytools2[1438:60b] label should be  6:6:2
2014-06-18 11:52:27.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:28.616 mytools2[1438:60b] label should be  6:6:1
2014-06-18 11:52:28.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:29.616 mytools2[1438:60b] label should be  6:6:0
2014-06-18 11:52:29.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:30.615 mytools2[1438:60b] label should be  6:5:59
2014-06-18 11:52:30.616 mytools2[1438:60b] label is  (null)
2014-06-18 11:52:30.773 mytools2[1438:60b] turn timer OFF

Upvotes: 0

Views: 795

Answers (1)

nburk
nburk

Reputation: 22731

You can use reloadRowsAtIndexPaths:withRowAnimation: to only update the cell that you need to be updated (the one that contains the label), instead of updating the whole table:

[self.tableView reloadRowsAtIndexPaths:@[yourIndexPath] withRowAnimation: UITableViewRowAnimationNone]

Upvotes: 2

Related Questions