2012-08-07 55 views
2

我有一个UITableViewCell内的标签计时器,使用此代码如何更新里面的UITableViewCell标签计时器?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [[UITableViewCell alloc]init]; 

    timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 26, 280, 25)]; 
    timeLabel.text = [timeArray objectAtIndex:indexPath.row]; 
    timeLabel.backgroundColor = [UIColor clearColor]; 
    timeLabel.textColor = [UIColor blackColor]; 

    [cell.contentView addSubview:timeLabel]; 
    return cell; 
    NSLog(@"Title %@, time %@, second %@, time %i, tag %d", titleArray, timeArray, secondArray, time, button.tag); 
} 

而且我有一个按钮使用此代码

- (void)onoffBtn:(id)sender 
{ 
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerElapsed:) userInfo:nil repeats:YES]; 
} 

- (void) timerElapsed:(id)timer 
{ 
    if (time == 0) 
    { 
     [countdownTimer invalidate]; 
    } 
    // this part only code for counting down timer 
    NSLog(@"%d:%d:%d", hour , minute, second); 
} 

如果我把timeLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hour , minute, second];timerElapsed,当然启动一个定时器,它不会更新我的标签内的单元格。

那么如何更新我的内细胞标签每一秒?

回答

1

timeLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hour , minute, second];cellForRowAtIndexPath和打电话给你的tableview内timerElapsed的reloadData方法。

我相信你的小时,分​​钟和秒包含更新后的值。

+0

是的,你们俩是正确的1个单元,但心不是权利,如果大于1多细胞的标记仅只有拉斯细胞更新。 – Piyo 2012-08-07 08:46:00

+0

如果每个小区包含不同的小时,分​​钟,秒的值则应使用阵列将包含值针对每个小区和在的cellForRowAtIndexPath您应该从阵列[hourArr objectAtIndex:indexPath.row]提供值等on..where hourArr是一个数组明白了吗?在提问时你应该清楚。 – 2012-08-08 04:55:21

+0

谢谢!我知道了。现在它完美的工作! – Piyo 2012-08-08 05:34:16

2

您可以使用此更新表格:

[theTable reloadData] 

确保重装然后重建表的单元格时,得到正确的价值观

编辑: 别忘了回收细胞像这样...

 static NSString *kDisplayCell_ID = @"DisplayCellID"; 
     cell = [self.tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID]; 
     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDisplayCell_ID]; 
     } 
     .... 

你可能想看看如何

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

需要实现

+0

请检查答案 - 它看起来好像你忘了回收细胞 – user387184 2012-08-07 10:53:06