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

查看:205
本文介绍了获取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);

然后,您可以放心使用

[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或UIScrollView

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

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