The problem here is that everytime a cell gets reloaded, you initialize a new text view which won't have the text that was previously entered. The best solution to this problem is for you to keep an NSMutableArray* that stores the user's input. You have different options, i'll just describe the steps for one possible solution:
- declare an NSMutableArray property for storing the user inputs as follows:
@property (strong, nonatomic) NSMutableArray *userData;
and the getter that performs lazy initialisation:
-(NSMutableArray *)userData
{
if(!_userData){
_userData = [[NSMutableArray alloc] initWithCapacity:[self.tableData count]];
for (int i = 0; i < [self.tableData count]; i++)
[_userData addObject:@""];
}
return _userData;
}
- Use the textfield's property
tag
to store the row for the particular textfield as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell%d", indexPath.row ]];
if(cell == nil)
cell = [[UITableViewCell alloc] init];
cell.textLabel.text = _tableData[indexPath.row];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.delegate = self;
textField.tag = indexPath.row;
if (indexPath.row == 0) {
textField.placeholder = @"(Required)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 1) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 185, 30)];
label.text = @"(Required)";
label.textColor = [UIColor lightGrayColor];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
secondCellField = label;
secondCell = cell;
} else if (indexPath.row == 2) {
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 3) {
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 4) {
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 5) {
textField.placeholder = @"(Optional)";
[cell.contentView addSubview:textField];
} else if (indexPath.row == 6) {
UISwitch *switch = [[UISwitch alloc] initWithFrame:CGRectMake(210, 8, 50, 30)];
[switch addTarget:self action:@selector(segwayToWork) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switch];
_workSwitch = switch;
}
if(![[self.userData objectAtIndex:indexPath.row] isEqualToString:@""]){
NSLog(@"%@ at indexPath.row %d",[self.userData objectAtIndex:indexPath.row], indexPath.row);
textField.placeholder = nil;
textField.text = [self.userData objectAtIndex:indexPath.row];
}
return cell;
}
- Finally, in your UITextField delegate, you can do something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.userData[textField.tag] = textField.text;
return YES;
}
Now when a cell will be reloaded, it will first check whether there was already a user input, and if there was one, it will set the placeholder to nil and will instead show just the user input.