2012-08-07 60 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