0

My tableview cell contain one label and one textfield, I am reusing my cell and it is working. but when I enter text in first textfield and hit return I want to go to next textfield, how can I handle that?

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Bevan
  • 342
  • 2
  • 14

3 Answers3

1

Here's what I did:

  1. Created a delegate protocol with these methods:

    - (void)textFieldInCellDidReturn:(UITableViewCell*)cell;
    - (void)textFieldInCellDidBeginEditing:(UITableViewCell*)cell;
    - (void)textFieldInCellDidEndEditing:(UITableViewCell*)cell;
    
  2. In the cell:

    -(BOOL)becomeFirstResponder
    {
        return [self.inputTextField becomeFirstResponder];
    }
    
    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {
        if ([self.delegate respondsToSelector:@selector(textFieldInCellDidBeginEditing:)])
        {
            [self.delegate textFieldInCellDidBeginEditing:self];
        }
    }
    -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        if ([self.delegate respondsToSelector:@selector(textFieldInCellDidEndEditing:)])
        {
            [self.delegate textFieldInCellDidEndEditing:self];
        }
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        if ([self.delegate respondsToSelector:@selector(textFieldInCellDidReturn:)])
        {
            [self.delegate textFieldInCellDidReturn:self];
        }
        return NO;
    }
    
  3. In the view controller and table view delegate, declare the iVar NSIndexPath* indexPathOfFirstResponder and adopt the delegate

    - (void)textFieldInCellDidBeginEditing:(UITableViewCell *)cell
    {
        indexPathOfFirstResponder = [self.theTable indexPathForCell:cell];
    }
    
    -(void)textFieldInCellDidReturn:(UITableViewCell *)cell
    {
        NSIndexPath* indexPathForCell = [self.theTable indexPathForCell:cell];
        if(indexPathForCell.row < [self.theTable numberOfRowsInSection:indexPathForCell.section]-1)
        {
            NSIndexPath* nextIndexPathInSection = [NSIndexPath indexPathForRow:indexPathForCell.row+1 inSection:indexPathForCell.section];
            UITableViewCell* nextCellInSection = [self.theTable cellForRowAtIndexPath:nextIndexPathInSection];
            if (nextCellInSection)
            {
                [nextCellInSection becomeFirstResponder];
            }
            else
            {
                indexPathOfFirstResponder = nextIndexPathInSection;
                [self.theTable scrollToRowAtIndexPath:nextIndexPathInSection atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
            }
        }
        else
        {
            [self.theTable endEditing:YES];
        }
    }
    
    -(void)textFieldInCellDidEndEditing:(UITableViewCell *)cell
    {
        indexPathOfFirstResponder = nil;
    }
    
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([indexPath isEqual:indexPathOfFirstResponder])
        {
            [cell becomeFirstResponder];
        }
    }
    
Ahmed Hamed
  • 504
  • 1
  • 3
  • 11
0

Set sequential tag for the textField e.g. 0,1,2..... Import textField delegate method textFieldDidEndEditing. Whenever return key is pressed, this method gets called.

Use something like this:

-(void)textFieldDidEndEditing:(UITextField *)textField{
   if(textField.tag == currentTextFieldTag+1){
    [textField becomesFirstResponder];
}
}
Vikas Mishra
  • 246
  • 1
  • 12
0

in your cell for row function use this code

cell.textField.tag = indexPath.row;

Now implement a UITextFieldDelegate Method

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag+1 inSection:YOUR_SECTION];

UITableViewCell* cell = [yourTable cellForRowAtIndexPath:indexPath];
[cell.textField becomesFirstResponder]; 


 // if you need  [yourTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
     }

I hope this will help.Thankyou

baydi
  • 1,003
  • 6
  • 11