有人可以解釋爲什麼你應該使用viewWithTag
從dequeueReusableCellWithIdentifier
的單元格獲得子視圖(例如UILabel
等)?我有一個自定義UITableViewCell
與其中幾個UILabel
s(我已經轉載了下面的簡單版本)。這些標籤在關聯的NIB文件中定義,並用IBOutlet
s聲明並鏈接回自定義單元的控制器類。在桌面的dequeueReusableCellWithIdentifier
,我這樣做:爲什麼在「dequeueReusableCellWithIdentifier」中使用「viewWithTag」?
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設置)。
有人可以告訴我哪種方法是首選,爲什麼?
謝謝!
通常,您應該使用UITableViewCell子類來使用快速和骯髒的viewWithTag方法。 – 2013-07-10 01:16:31