Q:

UITextView on cell clears when scrolling

Hello fellow programmers,
I am really cracking my head trying to understand what the problem is, but I just can't seem to figure out the problem. I have done research on this but nothing works for me unfortunately. The problem is that I have multiple cells and each cell is given a specific UITextField which I draw on the cellForRowAtIndexPath programmatically. The issues is that when I enter some information on that any Text Field and I scroll down on the table view, the text on the text field disappears into thin air. I am trying to understand why the cell reuse (which I believe is causing the problem), is causing this.
Here is my code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }

    cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
    cellText.font = [UIFont boldSystemFontOfSize:11.0];
    cellText.textColor = [UIColor grayColor];
    [cell.contentView addSubview:cellText];

    if (indexPath.section == 0)
    {
        cellText.text = clientInfoArray[indexPath.row];

        if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:@"Customer Name"])
        {
            nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            nameTxtField.placeholder = @"full name";
            nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            [nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            [cell addSubview:nameTxtField];
        }
        if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:@"Phone"])
        {
            phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            phoneTxtField.placeholder = @"xxx-xxx-xxx";
            phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
            [phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            [cell addSubview:phoneTxtField];
        }
        if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:@"Date"])
        {
            dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:dateLabel];
            dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
        }
    }
    if (indexPath.section == 1) {

        cellText.text = vehicleInfoArray[indexPath.row];

        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Make and Model"])
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:modelLabel];
            modelLabel.text = makeAndModelData;

        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Color"])
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:colorLabel];
            colorLabel.text = colorData;
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Doors"])
        {
            doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            doorsTxtField.placeholder = @"#";
            doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            [doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:doorsTxtField];
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Vehicle VIN"])
        {
            vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            vinTxtField.placeholder = @"#";
            vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            [vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:vinTxtField];
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Fuel"])
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:fuelLabel];
            fuelLabel.text = fuelData;
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Milage"])
        {
            milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            milageLabel.placeholder = @"ODO";
            milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
            milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
            [milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
            milageLabel.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:milageLabel];
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Other description"])
        {
            otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
            otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
            [cell addSubview:otherDTxtView];
        }
    }
    if (indexPath.section == 2) {

        cellText.text = InAndOutInfoArray[indexPath.row];
    }
    return cell;
}

Any ideas on how I can fix this problem? + Extra points for a helpful answer. :)

A:

Whenever you are using reuse cell techniques, the cell that is not visible will be deallocated to free some memory.
Since you are creating a textfield in the cellForRow method, it will also be deallocated when the cell is not visible and redrawn when it becomes visible.
You could save the textfield value in some array (using textfield's didEndEditing delegate) and use that array to populate the text during textfield creation process inside the cellForRow method.

