【问题标题】:Objective C - iOS, iterate through UILabels within static table cells off screenObjective C - iOS,在屏幕外的静态表格单元格中遍历 UILabel
【发布时间】:2012-04-22 21:34:59
【问题描述】:
我有一个由静态单元格组成的 UITableView,每个单元格都包含一个 UILabel,它在屏幕加载时填充了字段数据。单元格的数量超出了一个屏幕的容量,因此表格视图会滚动。 UILabel 在设计时是隐藏的,我想在设置完所有文本属性后将它们设置为可见。我一直在使用 tableView 的 subviews 属性来遍历标签到 setHidden:NO 但这只会影响当前正在查看的单元格中的标签。无论哪些 UILabel 是否在视图中,我如何遍历所有 UILabel?
谢谢
乔纳森
【问题讨论】:
标签:
objective-c
ios
uitableview
uilabel
【解决方案1】:
您可以在 tableView:cellForRowAtIndexPath: 方法中解决此问题。
假设您已经实现了静态单元格the way Apple's guide suggests,您的tableView:cellForRowAtIndexPath: 应该看起来像一系列if-then-else 语句,返回通过IBOutlet 对象提供的单元格:
if (indexPath.row == 0) {
return cell1;
} else if (indexPath.row == 1) {
return cell2;
} // .. and so on
修改这段代码如下:
UITableViewCell *res = nil;
if (indexPath.row == 0) {
res = cell1;
} else if (indexPath.row == 1) {
res = cell2;
} // .. and so on
// Call your custom code that makes the label visible
if (allTextPropertiesHaveBeenLoaded) {
[res setMyLabelVisible];
}
return res;
设置所有文本属性后,调用reloadData 强制所有单元格通过您的tableView:cellForRowAtIndexPath: 并重新配置。
【解决方案2】:
只需调用 cellForRowAtIndexPath: 方法
for(NSUInteger i = 0; i < numberOfCells; i++) {
UITableView* cell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:i inSection:0];
}