【问题标题】:How can I get a custom UITableView to automatically scroll to a selected text field?如何让自定义 UITableView 自动滚动到选定的文本字段?
【发布时间】:2010-06-08 08:27:17
【问题描述】:

我有一个UITableView,我通过自定义UIViewController 控制它。当用户单击“添加”按钮时,我向UITableView 添加一行,其中包含一个文本字段,并使其成为第一响应者。问题是,当表格底部不可见(或被键盘隐藏)时,UITableView 不会滚动以显示文本字段。

UITableViewController 自动执行此操作,但我的视图控制器不能是 UITableViewController 的子类。

【问题讨论】:

标签: iphone uitableview uitextfield


【解决方案1】:

当键盘出现或消失时,我通过在 UITableView 上旋转 contentInset 来解决此问题。

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
    keyboardHeight = keyboardBounds.size.height;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
    [UIView commitAnimations];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0]
                     atScrollPosition:UITableViewScrollPositionMiddle
                             animated:YES];
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    tableView.contentInset = UIEdgeInsetsZero;
    [UIView commitAnimations];
}

在加载UITableView 时调用registerForKeyboardNotifications,其他一切都应该正常工作。

【讨论】:

  • [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0] atScrollPosition:UITableViewScrollPositionMiddle 动画:YES];这里的“项目”是什么意思?
  • 如果有人对 UIKit 在这种情况下如何处理动画感兴趣(我遇到了同样的问题),请在此处阅读:cocoabuilder.com/archive/cocoa/…
【解决方案2】:

我找到了-scrollToRowAtIndexPath:atScrollPosition:animated:,我猜这会满足你的要求。

【讨论】:

  • 没有,因为UITableView 仍然是整个屏幕的高度(并且被键盘覆盖)。
【解决方案3】:
【解决方案4】:

为 ios5 工作

UITextField *activeField;

// Called when the UIKeyboardDidShowNotification is sent.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   activeField = nil;
}

// Called when the UIKeyboardWillHideNotification is sent

- (void)keyboardWasShown:(NSNotification *)aNotification {


   id view = [activeField superview];
   while (view && ![view isKindOfClass:[UITableViewCell class]]) 
   {
      view = [view superview];
   }
   UITableViewCell *cell = view;
   NSIndexPath *indexPath = [tableview indexPathForCell:cell];
   CGRect keyboardBounds;
   [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationBeginsFromCurrentState:YES];
   tableview.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
   [UIView commitAnimations];
   [tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationBeginsFromCurrentState:YES];
    tableview.contentInset = UIEdgeInsetsZero;
   [UIView commitAnimations];
}

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 1970-01-01
    • 2014-10-11
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多