>我有一个由静态单元格组成的UITableView,每个单元格都包含一个UILabel,当屏幕加载时,该UILabel填充了字段数据。 单元格数超过一个屏幕所能容纳的数量,因此表格视图会滚动。 UILabel 在设计时是隐藏的,我想在设置完所有文本属性后将它们设置为可见。 我一直在使用 tableView 的 subviews 属性来迭代标签以设置隐藏:NO,但这只会影响当前视图中单元格内的标签。 如何遍历所有 UILabels,无论哪些 UILabel 在视图中?
谢谢乔纳森
tableView:cellForRowAtIndexPath:
方法中解决此问题。
假设您已经按照 Apple 指南的建议实现了静态单元格,您的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:
并重新配置。
只需调用 cellForRowAtIndexPath: 方法
for(NSUInteger i = 0; i < numberOfCells; i++) {
UITableView* cell = [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:i inSection:0];
}