I have a UITableViewCell
designed via a Storyboard that contains a UITextField
and UILabel
. I want to update the values of these views when cellForRowAtIndexPath
is called.
I first tried to get references to the textfield and label like this:
UITextField *textfield = (UITextField*)[cell.contentView viewWithTag:10];
UILabel *label = (UILabel*)[cell.contentView viewWithTag:20];
This worked for the UILabel
, but I was only able to get the reference to UITextField
the first time the cell was created, I was able to set the original value (textField.text
) but never update it. If I inspect the array of subviews I can see that the UITextField
is there, I just can't get to it with viewWithTag
. I switched to this (below) and it works:
UITextField* textField = [[cell.contentView subviews] objectAtIndex:0];
UILabel *label = [[cell.contentView subviews] objectAtIndex:1];
Why doesn't the first approach work? What's the right way to do this?
This code is in the context of:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCell" forIndexPath:indexPath];
UITextField* textField = [[cell.contentView subviews] objectAtIndex:0];
textField.text = @"<something>";
UILabel *label = [[cell.contentView subviews] objectAtIndex:1];
label.text = @"<something else>";
return cell;
}