I've tried a few of the solutions in this thread, but I'm having trouble. My table is loaded dynamically with data from plists, so I cannot create connections from one cell to another in storyboard. I implemented a custom UITableViewCell class called DSCell which has two DSTextField objects in it on the right side of the cell. When hitting enter on the leftmost DSTextField, it successfully shifts focus to the next field. But, when hitting enter on the right text field, it should move focus to the text field in the next cell (one row down). But it doesn't.
The text fields in the cells have the tags 2 and 3.
Here is my cellForRowAtIndex method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PaperCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSString *text = [_paper objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = text;
// Set the "nextField" property of the second DSTextfield in the previous cell to the first DSTextField
// in the current cell
if(indexPath.row > 0)
{
DSCell *lastcell = (DSCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]];
DSTextField *lastField = (DSTextField *)[lastcell viewWithTag:3];
DSTextField *currentField = (DSTextField *)[cell viewWithTag:2];
lastField.nextField = currentField;
}
return cell;
}
Here's the textFieldShouldReturn method:
- (BOOL) textFieldShouldReturn:(UITextField *) textField {
DSTextField *field = (DSTextField *)textField;
UIResponder *responder = field;
[responder resignFirstResponder];
responder = field.nextField;
[responder becomeFirstResponder];
return YES;
}
Currently I'm attempting to set the nextField property of the second DSTextField to the current cell when cellForRowAtIndexPath is called, but it does not seem to work. I start at row 1 and attempt to retrieve the cell in the previous row, then assign the rightmost text field's nextField property to the leftmost text field in the current cell.
Is there a better way to do this? I don't want to have different tags for every single text field and do it that way, that could get messy.