can someone please explain why you should use viewWithTag
to get subviews (e.g. UILabel
etc) from a cell in dequeueReusableCellWithIdentifier
?
Some background info: I've got a custom UITableViewCell
with a couple of UILabel
s in it (I've reproduced a simple version of this below). These labels are defined in the associated NIB file and are declared with IBOutlet
s and linked back to the custom cell's controller class. In the tableview's dequeueReusableCellWithIdentifier
, I'm doing this:
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;
Everything works fine. However from the tutorials I've seen, it looks like when changing the labels' values I should be doing this instead:
UILabel *firstLabel = (UILabel *)[customCell.contentView viewWithTag:555];
firstLabel.text = @"Hello";
UILabel *secondLabel = (UILabel *)[customCell.contentView viewWithTag:556];
secondLabel.text = @"World!";
(The labels' tag values have been set in the NIB).
Can someone tell me which method is preferred and why?
Thanks!