views:

132

answers:

4

Hi, 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 UILabels in it (I've reproduced a simple version of this below). These labels are defined in the associated NIB file and are declared with IBOutlets 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!

A: 

I always hook subviews to properties of my UITableViewCell subclass via IBOutlets, as you have done. I can't think of any good reason to use viewWithTag.

cduhn
A: 

I always use IBOutlet as your example. If you have IBOutlet connections available, then there is no need to use tag. May be the tutorial have not used IBOutlet. So it is accessing via tag.

Using IBOutlet seems more readable to me. Only exceptions is when I try to access a number of views through a loop. Say I have 10 UILabels in view and I want to access them in a loop. Here using tag seems simple. And this is true for all UIView, not only for UITableViewCell.

taskinoor
A: 

viewWithTag: is just a quick and dirty way to pull out child views without having to set up IBOutlet properties on the parent, or even without having to create a UITableViewCell subclass.

For very simple cases this is an acceptable solution, that's what viewWithTag: was intended for. However if you are going to reuse that cell a lot or you want it to have a more developer-friendly interface then you will want to subclass and use real properties as in your first example.

So use viewWithTag: if it's a very simple cell you designed in IB with no subclass and with just a couple of labels. Use a cell subclass with real properties for anything more substantial.

Mike Weller
A: 

I've realised that it's useful to retrieve elements using "viewWithTag" if the elements were added to the cell programmatically (i.e. not defined in a NIB and hooked-up via IBOutlets)—this prevents multiple labels etc. to be created for each instance of the cell.

cravr