我有一个标准的表格 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 中有一个索引行,所以就得到 p>
TimerCell *cell by cellForItemAtIndexPath 并仅更新此单元格。
关于ios - 是否可以刷新 UITableViewCell 中的一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995916/