【问题标题】:How do I update label timer inside UITableViewCell?如何更新 UITableViewCell 中的标签计时器?
【发布时间】:2012-08-07 06:02:11
【问题描述】:

我在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,当然它不会更新我在单元格内的标签。

那么如何每秒更新单元格内的标签?

【问题讨论】:

    标签: iphone ios5 xcode4.3


    【解决方案1】:

    你可以用这个来更新你的表:

    [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
    {
    

    需要实施

    【讨论】:

    • 请检查答案 - 好像你忘了回收细胞
    【解决方案2】:

    timeLabel.text = [NSString stringWithFormat:@"%d:%d:%d", hour , minute, second]; 放入cellForRowAtIndexPath 并在timerElapsed 中调用tableview 的reloadData 方法。

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

    【讨论】:

    • 是的,你俩都适合 1 个单元格,但如果单元格超过 1 个则不正确。标签仅更新 las 单元格。
    • 如果您的每个单元格包含不同的小时、分钟、秒值,那么您应该使用包含每个单元格的值的数组,并且在 cellForRowAtIndexPath 中您应该将数组中的值提供为 [hourArr objectAtIndex:indexPath.row]等等..其中 hourArr 是一个数组 明白了吗?提问时要清楚。
    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多