UITextField in UITableViewCell Help

To solve your problem you have to maintain an array, with some number (number of textFields you added to all cells) of objects.

While creating that array you need add empty NSString objects to that array. And each time while loading the cell you have to replace the respected object to respected textField.

Check the following code.

- (void)viewDidLoad{
    textFieldValuesArray = [[NSMutableArray alloc]init];
    for(int i=0; i<numberofRows*numberofSections; i++){
        [textFieldValuesArray addObject:@""];
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return numberofSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return numberofRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

     CustomTextField *tf = [[CustomTextField alloc] initWithFrame:CGRectMake(5,5,290,34)];
     tf.tag = 1;
     [cell.contentView addSubView:tf];
     [tf release];
    }
    CustomTextField *tf = (CustomTextField*)[cell viewWithTag:1];
    tf.index = numberofSections*indexPath.section+indexPath.row;
    tf.text = [textFieldValuesArray objectAtIndex:tf.index];

    return cell;
    }

- (void)textFieldDidEndEditing:(UITextField *)textField{

    int index = textField.index;
    [textFieldValuesArray replaceObjectAtIndex:index withObject:textField.text];
}

Leave a Comment