我正在tableview控制器中創建一個帶有textfields的窗體。我想,當返回鍵的字段中輸入值後按在這裏實現兩件事情:帶文本區域的UiTableView - 出現鍵盤時滾動
1)將光標移動到下一個字段 2)滾動的tableview了
我的代碼正在爲移動光標移到下一個字段但滾動部分不起作用。你能不能讓我知道我在這裏失蹤了什麼。我研究了堆棧溢出的類似問題,並且從他們那裏得到了一些建議,但仍然面臨着問題。欣賞你的投入。
- (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 ;
}
那是因爲你的ContentSize不夠大,嘗試並增加你的UITableView ContentSize。 – Bonnie