2

我的 TableView 中有两个文本字段。When one is selected for editing, the Textfield is moving to the center of the screen. 这就是我所做的:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField == self.nameTextField) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0  inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
    else if(textField == self.passwordTextField) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

一切正常,但只有在键盘打开时。如果我第一次单击文本字段或键盘已关闭,则不会发生滚动。

谁能告诉我为什么或者你有其他解决方案吗?

非常感谢!

4

1 回答 1

0

尝试 textFieldShouldReturn 方法,它会在键盘按下时被调用。以下逻辑适用于我,将此方法添加到您的代码中:

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

[textField resignFirstResponder];
[self.tableView setScrollEnabled:YES];
[self.tableView setContentOffset:CGPointZero animated:NO];
return YES;

}

希望这对您有所帮助。

于 2012-11-01T10:28:13.980 回答