获取 UITableView 以滚动到选定的 UITextField 并避免被键盘隐藏 [英] Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard

查看:22
本文介绍了获取 UITableView 以滚动到选定的 UITextField 并避免被键盘隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIViewController(不是 UITableViewController)的表视图中有一个 UITextField.如果表格视图位于 UITableViewController 上,表格将自动滚动到正在编辑的 textField 以防止它被键盘隐藏.但在 UIViewController 上却没有.

I have a UITextField in a table view on a UIViewController (not a UITableViewController). If the table view is on a UITableViewController, the table will automatically scroll to the textField being edited to prevent it from being hidden by the keyboard. But on a UIViewController it does not.

我已经尝试了几天阅读多种方法来尝试实现这一点,但我无法让它发挥作用.最接近实际滚动的是:

I have tried for a couple of days reading through multiple ways to try to accomplish this and I cannot get it to work. The closest thing that actually scrolls is:

-(void) textFieldDidBeginEditing:(UITextField *)textField {

// SUPPOSEDLY Scroll to the current text field

CGRect textFieldRect = [textField frame];
[self.wordsTableView scrollRectToVisible:textFieldRect animated:YES];

}

但是,这只会将表格滚动到最上面一行.几天的挫败感似乎是一件容易的事.

However this only scrolls the table to the topmost row. What seems like an easy task has been a couple of days of frustration.

我正在使用以下内容来构建 tableView 单元格:

I am using the following to construct the tableView cells:

- (UITableViewCell *)tableView:(UITableView *)aTableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] 
        initWithStyle:UITableViewCellStyleDefault 
        reuseIdentifier:identifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryNone;

        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(180, 10, 130, 25)];

        theTextField.adjustsFontSizeToFitWidth = YES;
        theTextField.textColor = [UIColor redColor];
        theTextField.text = [textFieldArray objectAtIndex:indexPath.row];
        theTextField.keyboardType = UIKeyboardTypeDefault;
        theTextField.returnKeyType = UIReturnKeyDone;
        theTextField.font = [UIFont boldSystemFontOfSize:14];
        theTextField.backgroundColor = [UIColor whiteColor];
        theTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        theTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        theTextField.clearsOnBeginEditing = NO;
        theTextField.textAlignment = UITextAlignmentLeft;

        //theTextField.tag = 0;
        theTextField.tag=indexPath.row;

        theTextField.delegate = self;

        theTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [theTextField setEnabled: YES];

        [cell addSubview:theTextField];

        [theTextField release];


}

return cell;
}

如果我能以某种方式在 textFieldDidBeginEditing 方法中传递 indexPath.row,我怀疑我可以让 tableView 正确滚动?

I suspect I can get the tableView to scroll properly if I can somehow pass the indexPath.row in the textFieldDidBeginEditing method?

感谢任何帮助.

推荐答案

在我的应用程序中,我成功地使用了 contentInsetscrollToRowAtIndexPath 的组合,如下所示:

In my app, I have successfully used a combination of contentInset and scrollToRowAtIndexPath like this:

当你想显示键盘时,只需在底部添加一个 contentInset 和你想要的高度:

When you want to display the keyboard, just add a contentInset at the bottom with your table with desired height:

tableView.contentInset =  UIEdgeInsetsMake(0, 0, height, 0);

那么,你就可以放心使用了

Then, you can safely use

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:cell_index inSection:cell_section] animated:YES];

通过添加 contentInset,即使您专注于最后一个单元格,tableView 仍然可以滚动.只需确保在关闭键盘时重置 contentInset.

By adding the contentInset, even if you are focusing on the last cell the tableView will still be able to scroll. Just make sure that when you are dismissing the keyboard, you reset the contentInset.


如果您只有一个部分(您可以将 cell_section 替换为 0)并使用 textView 标签通知单元格行.


If you have only one section (you can replace cell_section with 0) and the use the textView tag to inform the cell row.

这篇关于获取 UITableView 以滚动到选定的 UITextField 并避免被键盘隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆