ios - 是否可以刷新 UITableViewCell 中的一个 View

标签 ios objective-c cocoa-touch uitableview

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

cellForRowAtIndexPath 中,我识别出我需要更新哪个单元格并将 weak 属性分配给此类单元格内的标签。

然后,我调用

label.text = @"New value";

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

我不想调用reloadRowsAtIndexPaths,我只需要刷新单元格 View 中的一个 subview 。是否可以?

更新:

这是我的代码:

这是 View Controller .m 文件中的属性定义:

@property (nonatomic, weak) UILabel* runningTimerLabel;

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

- (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; 
}

这是更新标签的方法:

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

此方法正在从后台线程调用:

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

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

    }
}

最佳答案

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

你有 self.runningTimer 所以你的 tableView 中有一个索引行,所以就得到

TimerCell *cell by cellForItemAtIndexPath 并仅更新此单元格。

关于ios - 是否可以刷新 UITableViewCell 中的一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995916/

相关文章:

ios - 无法使用其文档从 parse.com 快速检索图片

ios - CLGeocoder 地理编码请求超出限制

objective-c - 链接 libxml 时出错

ios - Facebook 登录无效范围

iphone - 禁止在 subview 上接收来自父 View 的触摸

iphone - 监测接近度时是否可以不关闭显示?

objective-c - objective-c : Any issues in having 2 NSNotifications set up in a single class?

ios - UICollectionView 在调用 reloadData 时不会立即更新,而是在 30-60 秒后随机更新

objective-c - @property 问题 - Objective C

iphone - 激活手电筒应用程序的 LED