3

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;
}
ToddB
  • 2,490
  • 3
  • 23
  • 40
  • Your code doesn't show where you are setting the tags on the view. Also what is it that you're trying to achieve? When do you need to access these fields? Immediately after they are entered or after a save button is pressed or something else? There are better ways of doing this. – Rog Sep 18 '13 at 18:56
  • @Rog The tag values are set in the Storyboard using one of the inspectors. I have a table with cells and the cells have textfields. When a user enters a value in one of the textfields I want to update the values of the textfields in the other cells to reflect this. – ToddB Sep 18 '13 at 19:00
  • You realise `cellForRowAtIndexPath` is only called on cell creation or if the system needs to dequeue a cell (i.e. scrolling a tableview)? Judging by your description you only have one or a handful of cells hence why you haven't been able to update the textfield via that method since it is never called again. You should instead implement the `textFieldDidEndEditing` delegate method and grab / update your textfield from there. – Rog Sep 19 '13 at 04:11
  • I detect that the value in the textfield has changed using `textFieldDidEndEditing` and then I update the data source (it has the values for all the textfields) and tell the table to refresh. During refresh is when I expect `cellForRowAtIndexPath` to be called and where I can update the value for the textfield. I think I'm losing the value for the tag somehow, I have an idea to check! Thanks for your comments! – ToddB Sep 19 '13 at 19:13

1 Answers1

1

Sorry. This was just a bug at my end, I apologize for wasting your time. I was overriding the value for textField.tag that had been set in the Storyboard UI. (I relied on it being the value set in Storyboard which was not the case). The original code runs correctly. Still not sure which approach is better. Is it safer to assume a specific index or a specific tag value? Neither of these seems safe to me.

ToddB
  • 2,490
  • 3
  • 23
  • 40