title: Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard
body: <p>I have a <code>UITextField</code> in a table view on a <code>UIViewController</code> (not a <code>UITableViewController</code>).
  If the table view is on a <code>UITableViewController</code>, the table will automatically scroll to the <code>textField</code> being edited to prevent it from being hidden by the keyboard. But on a <code>UIViewController</code> it does not.</p>
  
  <p>I have tried for a couple of days reading through multiple ways to try to accomplish this and I cannot get it to work. The closest thing that actually scrolls is:</p>
  
  <pre><code>-(void) textFieldDidBeginEditing:(UITextField *)textField {
  
  // SUPPOSEDLY Scroll to the current text field
  
  CGRect textFieldRect = [textField frame];
  [self.wordsTableView scrollRectToVisible:textFieldRect animated:YES];
  
  }
  </code></pre>
  
  <p>However this only scrolls the table to the topmost row.
  What seems like an easy task has been a couple of days of frustration.</p>
  
  <p>I am using the following to construct the tableView cells:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)aTableView
      cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];
  
  UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
  
      if (cell == nil) {
  
          cell = [[[UITableViewCell alloc] 
          initWithStyle:UITableViewCellStyleDefault 
          reuseIdentifier:identifier] autorelease];
  
          cell.accessoryType = UITableViewCellAccessoryNone;
  
          UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 10, 130, 25)];
  
          theTextField.adjustsFontSizeToFitWidth = YES;
          theTextField.textColor = [UIColor redColor];
          theTextField.text = [textFieldArray objectAtIndex:indexPath.row];
          theTextField.keyboardType = UIKeyboardTypeDefault;
          theTextField.returnKeyType = UIReturnKeyDone;
          theTextField.font = [UIFont boldSystemFontOfSize:14];
          theTextField.backgroundColor = [UIColor whiteColor];
          theTextField.autocorrectionType = UITextAutocorrectionTypeNo;
          theTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
          theTextField.clearsOnBeginEditing = NO;
          theTextField.textAlignment = UITextAlignmentLeft;
  
          //theTextField.tag = 0;
          theTextField.tag=indexPath.row;
  
          theTextField.delegate = self;
  
          theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
          [theTextField setEnabled: YES];
  
          [cell addSubview:theTextField];
  
          [theTextField release];
  
  
  }
  
  return cell;
  }
  </code></pre>
  
  <p>I suspect I can get the tableView to scroll properly if I can somehow pass the <code>indexPath.row</code> in the <code>textFieldDidBeginEditing</code> method?</p>
  
  <p>Any help is appreciated.</p>