2

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秒ごとに更新するにはどうすればよいですか?

4

2 に答える 2

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
{

実装する必要があります

于 2012-08-07T06:11:26.710 に答える
1

中に入れて、 timerElapsedtimeLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hour , minute, second];内にテーブルビューのメソッドをcellForRowAtIndexPath呼び出します。reloadData

あなたの時、分、秒には更新された値が含まれていると思います。

于 2012-08-07T06:15:27.433 に答える