Reputation: 801
I have a table view with 3 rows. Each row has the same custom tableviewcell with a uitextfield but the placeholder property differs.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"NewDestinationCell";
NewDestinationCell *cell = (NewDestinationCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[cellNib instantiateWithOwner:self options:nil];
cell = editCell;
self.editCell = nil;
cell.detailTextField.delegate = self;
cell.detailTextField.tag = indexPath.row;
[cell.detailTextField setInputAccessoryView:keybdToolbar];
}
if ([[textFieldStrings objectAtIndex:indexPath.row] length] != 0)
cell.detailTextField.text = [textFieldStrings objectAtIndex:indexPath.row];
return cell;
}
When I tap the text field and enter some text, go to another text field and then go back to the text field with my text entered, it erases what I typed and puts the placeholder text instead. Do I have to implement the commitEditingStyle method? The text field in each table row is linked to the same uitextfield outlet. Maybe that's why? Here is the code I'm using to traverse through the three rows.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField becomeFirstResponder];
textField.text = [textFieldStrings objectAtIndex:textField.tag];
currTextField = textField;
if (currTextField.tag == [self.tableView numberOfRowsInSection:0]-1) {
NextButton.enabled = NO;
PrevButton.enabled = YES;
}
if (currTextField.tag == 0) {
PrevButton.enabled = NO;
NextButton.enabled = YES;
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textFieldStrings replaceObjectAtIndex:textField.tag withObject:textField.text];
[textField resignFirstResponder];
}
- (IBAction)PrevTextField:(id)sender {
[tableViewCellFields[currTextField.tag-1] becomeFirstResponder];
}
- (IBAction)NextTextField:(id)sender {
[tableViewCellFields[currTextField.tag+1] becomeFirstResponder];
}
Upvotes: 2
Views: 1075
Reputation: 44633
Truth be told, that IBOutlet
is a bit confusing. But there are some issues here one of which is cell reuse.
Since the cells are reused, you shouldn't rely on text retaining their values and rather you should store what the typed in textFieldDidEndEditing:
method. Maintain an array for the values entered or not entered (using [NSNull null]
singleton). In cellForRowAtIndexPath:
method, if you see an existing text value set the text field's text to that value. This way you can counter the cell reuse effect.
Another issue is with the outlet StreetName
. When the cells are created, I am guessing StreetName
will point to the correct text field but what happens when the cells are reused. StreetName
would be pointing to the text field of the last cell that was created and as a result all assignments you do in cellForRowAtIndexPath:
are incorrect for reused cells. It would be a lot easier if you create a custom subclass of UITableViewCell
where you will do cell.myTextField.text = [textFieldStrings objectAtIndex:indexPath.row];
.
As a side note,
StreetName.delegate = self;
StreetName.tag = indexPath.row;
tableViewCellFields[indexPath.row] = StreetName;
[StreetName setInputAccessoryView:keybdToolbar];
The first and last line are something you only need to do once while creating the cell.
Upvotes: 1