0

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.

Community
  • 1
  • 1
Eric Amorde
  • 1,068
  • 9
  • 19

1 Answers1

1

I would suggest that you only try to find the correct cell to shift focus to in your textFieldShouldReturn: method. One thing that could be causing your problems is you might be requesting cells to be the lastCell that are not visible, which are then disposed of by the tableview (so the nextField isn't valid).

Altering the logic to happen when the thing returns (you'd still want to set the nextField between the two cells in a row):

- (BOOL) textFieldShouldReturn:(UITextField *) textField {

//This isn't necessary: UIResponder *responder = field;
//Or this: [responder resignFirstResponder];

//Check if it's the left or right text field
if (textField.tag == 3) {
    //Find the cell for this field (this is a bit brittle :/ )
    UITableViewCell *currentCell = textField.superview.superview;
    NSIndexPath *ip = [self.tableView indexPathForCell:currentCell];
    if (ip.row < [self.tableView numberOfRowsInSection:ip.section] - 1) {
        DSCell *nextCell = (DSCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:ip.row+1 inSection:ip.section]];
        [[nextCell viewWithTag:2] becomeFirstResponder];
    }
}

return YES;

}
Andrew Pouliot
  • 5,423
  • 1
  • 30
  • 34
  • You might also have to scroll the thing on screen. It's been a while since I've done much form-style UI :P – Andrew Pouliot Jan 25 '13 at 19:43
  • Hmm interesting. Right now, I'm just trying to hit enter on the very first cell so I haven't even tried to scroll. How can I prevent calling for a cell that doesn't exist (row + 1 being out of bounds when at the last row)? Also, why is it textField.superview.superview instead of just one superview call? I'll try what you suggested right now though, thank you. – Eric Amorde Jan 25 '13 at 19:53
  • @Eric (ip.row < cells - 1) takes care of not running off the end. Then, I assume you put your text fields in the `contentView` of the cell. – Andrew Pouliot Jan 25 '13 at 19:56