Display list of UITextField in UITableview


Question : How to make list of UITextField in UITableview ?

Review the below steps and try your own.

1. Implement one model class of .h and .m which is subclass of NSObject.   //Here Class name is TextFieldFormElement.h and TextFieldFormElement.m
2. Declare the property for displaying the value in UITextField.

   //Write your code here
   @interface TextFieldFormElement : NSObject 
   @property (strong, nonatomic) NSString *strtxtValue;

3. In your View Controller class make one method like below :

   -(void)initialize {

     // count is depends on total UITextField.
     // Initially display blank textField.
     for (int i=0; i<count; i++)
      {
         TextFieldFormElement *item = [[TextFieldFormElement alloc] init];
         item.strtxtValue = @"";
         [self.arrFormItems insertObject:item atIndex:i];

      }
      // Write you code here for set delegatea and  reload UITablebview.
     }

4. Implement UITableview Deleagate and Datasource method.

     //UITableview Datasource Method
     -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          
        // Write your code here
        // Configure the cell before it is displayed...
        TextFieldFormElement *objRecord = [self.arrFormItems objectAtIndex:indexPath.row];
        
        cell.objCurrentRecord = objRecord;

        // Here assign the value in TextField

         cell.txtCount.text = objRecord.strtxtValue;

         return cell;
     }

5. Update value in UITextField
    
   -(void)textFieldDidEndEditing:(UITextField *)textField{

    TextFieldFormElement *objFieldRecord = [self.arrFormItems objectAtIndex:textField.tag];
    
    objFieldRecord.strtxtValue = textField.text;
  }




You may also like

Leave a Reply