I am creating a form with textfields in a tableview controller. I am trying to achieve 2 things here when the return key is pressed after entering value for a field:
1) Move cursor to next field 2) Scroll the tableview up
My code is working for moving the cursor to the next field but the scroll part is not working. Could you let me know what I am missing here. I researched similar issues on stack overflow and am following suggestions from them but still facing issues. Appreciate your inputs.
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if ([textField isEqual:self.firstName]){
//Move cursor to next field
[self.lastName becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
if ([textField isEqual:self.lastName]){
//Move cursor to next field
[self.emailId becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
if ([textField isEqual:self.emailId]){
//Move cursor to next field
[self.phoneNumber becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
if ([textField isEqual:self.phoneNumber]){
//Move cursor to next field
[self.password becomeFirstResponder];
//scroll
id cellContainingFirstResponder = textField.superview.superview ;
NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
[self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
// This is the last field hence dismiss keyboard -> This part is working
if ([textField isEqual:self.password]){
[textField resignFirstResponder];
}
return YES ;
}
For scrolling the tableview up as per textfield's focus try this:
- (void) textFieldDidBeginEditing:(UITextField *)textField {
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[yourTableView scrollToRowAtIndexPath:[yourTableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
Here you can set scroll position to top by using UITableViewScrollPositionTop
You may wonder why two superviews are used for textfield
. One is for UITableViewCellContentView
and another is for UITableViewCell
so that you can grab the instance of your table's current/focused cell.
It works, we have used this in one of our projects
I used the viewcontroller as the data source for the table view. My solution:
Inside cellForRowAtIndexPath, add target to the textfield for UIControlEventEditingDidBegin control event:
[((YourCustomCell *) cell).textfield addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
Implement the named method: Find the cell for the textfield and scroll to that cell
- (void)textFieldDidBeginEditing:(UITextField *)textField{ for ( NSIndexPath *ip in [self.tableview indexPathsForVisibleRows] ){ UITableViewCell * cell = [self.tableview cellForRowAtIndexPath:ip]; if ( [cell isKindOfClass:[YourCustomCell class]]){ if ( [textField isEqual:((YourCustomCell *)cell).textfield] ){ [self.tableview selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionTop]; [self setContinueButtonEnabled:YES]; } } } }
You use TKKeyboardAvoidingTableview so no need to mannually scroll tableview it automatically scoll when cursor tap
you set xib in select tableview and set custom class like this
Try to use this code it may help you
CGPoint pt;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:scrView];
pt = rc.origin;
pt.x = 0;
pt.y =0;
[scrView setContentOffset:pt animated:YES];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.