是否可以刷新UITableViewCell中的一个视图 [英] Is it possible to refresh one view inside UITableViewCell

查看:48
本文介绍了是否可以刷新UITableViewCell中的一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的表格视图。在每个单元格 UITableViewCell 中,我都有一个标签。在任何给定的时间,我都需要为整个tableView不断更新一个标签(例如,想象一个时钟滴答声)。

I have a standard table view. Inside each cell UITableViewCell I have a label. At any given time I need to constantly updating one label for the whole tableView (imagine a clock ticking for example).

cellForRowAtIndexPath 我知道需要更新哪个单元格并将弱属性分配给此类单元格内的标签。

In the cellForRowAtIndexPath I recognize which cell I need to update and assign weak property to the label inside such cell.

然后,我呼叫


label.text = @新值;

label.text = @"New value";

并期望该特定单元格刷新该特定标签。我检查了弱引用,它是有效的并且文本正在更改,但是单元格没有刷新。

and expect for that particular cell to refresh that particular label. I checked the weak reference and it's valid and text is changing, however cell is not getting refreshed.

我不想调用 reloadRowsAtIndexPaths ,我只需要刷新单元格视图中的一个子视图即可。可能吗?

I don't want to call reloadRowsAtIndexPaths, I just need to refresh one subview inside cell view. Is it possible?

更新:

这是我的代码:

这是视图控制器.m文件中的属性定义:

This is property definition inside view controller .m file:

@property (nonatomic, weak) UILabel* runningTimerLabel;

我在 cellForIndex 调用内分配此属性:

I assign this property inside cellForIndex call:

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

    MKSlidingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"container"];
    TimerCell *foregroundCell = [tableView dequeueReusableCellWithIdentifier:kTimerCellId];
    UITableViewCell *backgroundCell = [tableView dequeueReusableCellWithIdentifier:@"background"];

    BOOL runningCell = (indexPath.row == self.runningTimer);

    if (runningCell) {
        self.runningTimerLabel = foregroundCell.time;
    }

    cell.foregroundView = foregroundCell;
    cell.drawerView = backgroundCell;
    cell.drawerRevealAmount = 146;
    cell.forwardInvocationsToForegroundView = YES;
    cell.delegate = self;
    cell.tag = indexPath.row;

    return cell; 
}

以下是更新标签的方法:

Here is the method that updates the label:

- (void)refreshRunningTimerCell
{
     self.runningTimerLabel.text = @"New text";
}

此方法是从后台线程调用的:

This method is getting called from a background thread:

- (void)refreshRunningTimer
{
    while (1) {
        sleep(_sleepTime);

        [self performSelectorOnMainThread: @selector(refreshRunningTimerCell)
                               withObject:nil waitUntilDone: YES];

    }
}


推荐答案

我真的不明白为什么要尝试在UIlabel上获取引用。

I really don't understand why you trying to get an reference on UIlabel.

您有self.runningTimer,因此tableView中有一个索引行,因此只需通过cellForItemAtIndexPath获取

You have self.runningTimer so you have a index row in your tableView, so just get

TimerCell * cell并更新此单元格即可。

TimerCell *cell by cellForItemAtIndexPath and just update this cell.

这篇关于是否可以刷新UITableViewCell中的一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆