How to activate next UITextField in a UITableView?

前端 未结 4 2074
逝去的感伤
逝去的感伤 2020-12-24 15:05

I am using this other SO answer for adding UITextFields to my UITableViewCells. However, I am now at a loss how I can let the user focus on the nex

4条回答
  •  情深已故
    2020-12-24 16:02

    This worked well for me, would have to be tweaked to fit your needs though:

    #pragma mark - UITextField Delegate
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
        HRCFormCell * currentCell = (HRCFormCell *) textField.superview.superview;
        NSIndexPath * currentIndexPath = [self.tableview indexPathForCell:currentCell];
    
        if (currentIndexPath.row != [self.questionsArray count] - 1) {
    
            NSIndexPath * nextIndexPath = [NSIndexPath indexPathForRow:currentIndexPath.row + 1 inSection:0];
            HRCFormCell * nextCell = (HRCFormCell *) [self.tableview cellForRowAtIndexPath:nextIndexPath];
    
            [self.tableview scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
    
            [nextCell.textField becomeFirstResponder];
        }
    
        return YES;
    }
    

提交回复
热议问题