views:

1223

answers:

1

I am not sure which one to use? cell.contentView sometimes give me weird errors like [uiview settext]...?? I was trying to setText to a UILabel*

+3  A: 

The one to use depends on how you created the cell in the first place. If you added your UILabel as a subview of the contentView (e.g. [cell.contentView addSubview:myLabel]), then you would use [cell.contentView viewWithTag:1] to retrieve it (assuming the label actually has a tag of 1).

However, I would caution you about using -viewWithTag: in the first place. It's fairly useful for poking at stuff, but it's not very reliable (because if two views have the same tag, you're only going to get one of them back) and it's not very efficient (it has to traverse the subviews every time you use it). I would instead recommend creating a UITableViewCell subclass that has properties for your custom views, so you can access them directly.

Kevin Ballard
+1 for the recommendation for subviews as ivars
Tim
thank you for your quick reply. I'd love to hear more about your alternate suggestion. Would you please give me more hints about that? Like a small sample of code would be very very useful for me right now. thanks!
SimpleCode
You've used ivars with other things, right? Just create a subclass of UITableViewCell and add your ivars to that. I would also recommend having this subclass set up its subviews in its -initWithStyle:reuseIdentifier: method. You should also declare @properties for these ivars so your controller can access them.When creating your cell in -tableView:cellForRowAtIndexPath: you can just cast the [tableView dequeueReusableCellWithIdentifier:@"foo"] call to (YourCellSubclass*).
Kevin Ballard
it works! thank you
SimpleCode