获取 UITableView 滚动到选定的 UITextField 并避免被键盘隐藏 [英] Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard
问题描述
我在 UIViewController
(不是 UITableViewController
)的表格视图中有一个 UITextField
.如果表格视图在 UITableViewController
上,表格将自动滚动到正在编辑的 textField
以防止它被键盘隐藏.但是在 UIViewController
上它没有.
我已经尝试了几天阅读多种方法来尝试实现这一点,但我无法让它发挥作用.实际滚动的最接近的东西是:
-(void) textFieldDidBeginEditing:(UITextField *)textField {//SUPPOSEDLY 滚动到当前文本字段CGRect textFieldRect = [文本框框];[self.wordsTableView scrollRectToVisible:textFieldRect 动画:YES];}
然而,这只会将表格滚动到最顶行.看似简单的任务却经历了几天的挫折.
我正在使用以下内容来构建 tableView 单元格:
- (UITableViewCell *)tableView:(UITableView *)aTableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {NSString *identifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition: 0], [indexPath indexAtPosition:1]];UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];如果(单元格 == 零){单元格 = [[[UITableViewCell 分配]initWithStyle:UITableViewCellStyleDefault重用标识符:标识符]自动释放];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];[单元格 addSubview:theTextField];[TextField 发布];}返回单元格;}
如果我能以某种方式在 textFieldDidBeginEditing
方法中传递 indexPath.row
,我怀疑我可以让 tableView 正确滚动?
感谢任何帮助.
在我的应用程序中,我成功地使用了 contentInset
和 scrollToRowAtIndexPath
的组合,如下所示:>
当你想显示键盘时,只需在表格底部添加一个具有所需高度的 contentInset:
tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0);
然后,您可以放心使用
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:cell_index inSection:cell_section] 动画:YES];
通过添加 contentInset,即使您关注最后一个单元格,tableView 仍然能够滚动.只需确保在关闭键盘时重置 contentInset.
如果您只有一个部分(您可以将 cell_section
替换为 0)并使用 textView 标记通知单元格行.
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.
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;
}
I suspect I can get the tableView to scroll properly if I can somehow pass the indexPath.row
in the textFieldDidBeginEditing
method?
Any help is appreciated.
In my app, I have successfully used a combination of contentInset
and scrollToRowAtIndexPath
like this:
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];
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.
EDIT:
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屋!