UITableViewCell倒计时UILabel没有更新

时间:2014-06-18 10:09:41

标签: ios objective-c uitableview timer uilabel

我想了解更多关于计时器的信息,所以我决定尝试创建一个Todo列表类型的IOS应用程序。在应用程序中,我希望允许用户能够根据需要为每个任务/项添加计时器。

到目前为止,用户可以选择一个时间(从选择器),启动和停止计时器没有任何问题。我遇到的问题是UILabel不会更新并显示倒数计时器文本。它更新时间的唯一时间是我进入子视图(编辑任务)然后返回主viewController(任务列表)。

我可以让单元格更新的唯一方法是在父viewController中使用[self.tableView reloadData]运行循环;我很确定这就像用大锤的方式杀死苍蝇一样。

任何见解都会很棒。感谢。

修改

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

我为tableview单元格创建了一个子类。这是我在taskTableViewCell.m文件中用于计时器的代码。

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

日志输出为:

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

1 个答案:

答案 0 :(得分:2)

您可以使用reloadRowsAtIndexPaths:withRowAnimation:仅更新需要更新的单元格(包含标签的单元格),而不是更新整个表格:

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