Creating Forms Using UITableView and UITextField | iOS Programmer Guide

To create a form that resembles the Gmail form in the iPhone Mail application, create a UITableView and add a UITextField into each UITableViewCell.
You can download the source code for an example here:
What follows is a brief explanation of some parts of the code.

Source Explanation

Our controller class has fields to hold references to the UITextFields and their values when they are modified.
  1. @interface FormTableController : UITableViewController<UITextFieldDelegate> {  
  2.     NSString* name_ ;  
  3.     NSString* address_ ;  
  4.     NSString* password_ ;  
  5.     NSString* description_ ;      
  6.       
  7.     UITextField* nameField_ ;  
  8.     UITextField* addressField_ ;  
  9.     UITextField* passwordField_ ;  
  10.     UITextField* descriptionField_ ;      
  11. }  
The makeTextField method creates a UITextField that resembles the ones used in the iPhone Mail application.
  1. -(UITextField*) makeTextField: (NSString*)text    
  2.                   placeholder: (NSString*)placeholder  {  
  3.     UITextField *tf = [[[UITextField alloc] init] autorelease];  
  4.     tf.placeholder = placeholder ;  
  5.     tf.text = text ;           
  6.     tf.autocorrectionType = UITextAutocorrectionTypeNo ;  
  7.     tf.autocapitalizationType = UITextAutocapitalizationTypeNone;  
  8.     tf.adjustsFontSizeToFitWidth = YES;  
  9.     tf.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];      
  10.     return tf ;  
  11. }  
The makeTextField is then called in the cellForRowAtIndexPath method to create the text fields, which are then added as subviews of the UITableCells.
  1. cell.textLabel.text = @"Name" ;  
  2. tf = nameField_ = [self makeTextField:self.name placeholder:@"John Appleseed"];  
  3. [cell addSubview:nameField_];  
The dimensions of the text field are also adjusted:
  1. // Textfield dimensions  
  2. tf.frame = CGRectMake(120, 12, 170, 30);  
When the value of any UITextField is changed, we save the new values by handling textFieldDidEndEditing.
  1. // Textfield value changed, store the new value.  
  2. - (void)textFieldDidEndEditing:(UITextField *)textField {  
  3.     if ( textField == nameField_ ) {  
  4.         self.name = textField.text ;  
  5.     } else if ( textField == addressField_ ) {  
  6.         self.address = textField.text ;  
  7.     } else if ( textField == passwordField_ ) {  
  8.         self.password = textField.text ;  
  9.     } else if ( textField == descriptionField_ ) {  
  10.         self.description = textField.text ;       
  11.     }  
  12. }  
Lastly, we dismiss the keyboard when the user taps Return by handling UIControlEventEditingDidEndOnExit.
  1. // Workaround to dismiss keyboard when Done/Return is tapped  
  2. [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];      
  1. // Workaround to hide keyboard when Done is tapped  
  2. - (IBAction)textFieldFinished:(id)sender {  
  3.     // [sender resignFirstResponder];  
  4. }