title: UITextField in UITableViewCell - adding new cells
body: <p>I am trying to create a table view similar to the YouTube video uploader view in the Photo Gallery on the iPhone.</p>
  
  <p>Here's the basic setup.</p>
  
  <p>I have a custom UITableViewCell created that contains a UITextField. Displaying the cell in my table works great and I can edit the text with no problems. I created an event hook so I can view when the text has changed in the text field.</p>
  
  <pre><code>[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]
  </code></pre>
  
  <p>What I want to do is this. When the user first edits the text I want to insert a new cell into the table view below the current cell (newIndexPath is calculated prior to the proper position):</p>
  
  <pre><code>[self.tableView beginUpdates];
  [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
  [self.tableView endUpdates];
  </code></pre>
  
  <p>Problem is when I run the cell insert code the cell is created but the text field's text updated briefly, but then the keyboard is dismissed and the text field is set back to an empty string.</p>
  
  <p>Any help would be awesome! I've been banging my head about this one all day.</p>
  
  <pre><code>- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
  {
      if (section == 0)
          return 2;
      else
          return self.tags.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      ...
      cell = (SimpleTextFieldTableCell *)[tableView dequeueReusableCellWithIdentifier:tagCellIdentifier];
  
      if (cell == nil)
      {
          cell = [[[NSBundle mainBundle] loadNibNamed:@"SimpleTextFieldTableCell" owner:nil options:nil] lastObject];
      }
  
      ((SimpleTextFieldTableCell *)cell).textField.delegate = self;
      ((SimpleTextFieldTableCell *)cell).textField.tag = indexPath.row;
      ((SimpleTextFieldTableCell *)cell).textField.text = [self.tags objectAtIndex:indexPath.row];
      [((SimpleTextFieldTableCell *)cell).textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  
      cell.selectionStyle = UITableViewCellSelectionStyleNone;
  }
  
  - (void)textFieldDidChange:(id)sender
  {
      UITextField *textField = sender;
  
      [self.tags replaceObjectAtIndex:textField.tag withObject:textField.text];
      if (textField.text.length == 1)
      {
          [textField setNeedsDisplay];
          [self addTagsCell];
      }
  }
  
  - (void)addTagsCell
  {
      NSString *newTag = @"";
      [self.tags addObject:newTag];
  
      NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.tags.count - 1 inSection:1];
      [self.tableView beginUpdates];
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
      [self.tableView endUpdates];
  }
  </code></pre>