【发布时间】:2010-09-03 02:22:48
【问题描述】:
有人能解释一下为什么你应该使用viewWithTag 从dequeueReusableCellWithIdentifier 的单元格中获取子视图(例如UILabel 等)吗?
一些背景信息:我有一个自定义的UITableViewCell,里面有几个UILabels(我在下面复制了一个简单的版本)。这些标签在关联的 NIB 文件中定义,并使用 IBOutlets 声明并链接回自定义单元的控制器类。在tableview的dequeueReusableCellWithIdentifier中,我正在这样做:
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];
if (customCell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
customCell = (CustomCell *)oneObject;
}
customCell.firstLabel.text = @"Hello";
customCell.secondLabel.text = @"World!";
return customCell;
一切正常。但是,从我看到的教程来看,在更改标签的值时,我应该这样做:
UILabel *firstLabel = (UILabel *)[customCell.contentView viewWithTag:555];
firstLabel.text = @"Hello";
UILabel *secondLabel = (UILabel *)[customCell.contentView viewWithTag:556];
secondLabel.text = @"World!";
(标签的标签值已在NIB中设置)。
谁能告诉我首选哪种方法以及为什么?
谢谢!
【问题讨论】:
标签: ios iphone uitableview