ios - 如何滚动到 UITableViewCell 的底部而不是 TextField

标签 ios objective-c uitableview storyboard

我有一个静态 tableView,每个 cell 中有一个 textfield。单元格高度为50。我有以下方法来处理键盘:

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
}

现在,当我输入 textField 时,如果它位于键盘下方,它会自动滚动到 textField。实际上,我不确定这是怎么发生的,因为我没有在任何地方配置这种行为。这里发生了什么?

我真正想要的是滚动到 cell 的底部,而不是 textField。目前,它滚动到 textField 但它切断了 cell 的一部分并且看起来不太好。

contentInset.bottom 是正确的,因为当键盘存在时,我可以完美地手动滚动到最后一个单元格。

我可能遗漏了一些 native 行为。

最佳答案

我会这样做(未测试)。在主视图的坐标系中找出单元格的框架。如果单元格底部低于键盘顶部,则调整表格 View 的内容偏移量...

UITableViewCell *cell = // get the cell containing the text field

CGRect bounds = [self.tableView convertRect:cell.frame toView:self.view];
CGFloat cellBottom = bounds.origin.y + bounds.size.height;
if (cellBottom > kbSize.height) {
    [self.tableView setContentOffset:(cellBottom-kbSize.height) animated:YES];
}

有几个 SO 答案可以帮助您从文本字段中获取单元格。最好的方法是沿着 super View 指针向上移动,直到找到 UITableViewCell。 Here's an example where I answered this for a collection view

关于ios - 如何滚动到 UITableViewCell 的底部而不是 TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556418/

相关文章:

ios - Firebase iOS Facebook 简单登录错误

ios - 事件指示器仅在表格单元格原型(prototype)中第一次旋转

ios - 我可以使用什么来永久替代 UUID?

ios - Tableview 的 contentView 小于 tableview frame 和 cell

ios - UITableView reloadSection 导致缺少显示的单元格

ios - 使用 accessoryView 为 UITableViewCell 的 backgroundColor 设置动画

ios - 无法从模态 ViewController 访问 UITabBarController

iphone - 如何在 iOS 5 上以横向模式显示视频?

objective-c - 在分析从 Swift 编译的二进制文件时,是否可以找出没有符号的函数的 Swift 方法名称?

ios - 无法使用 openURL 打开使用 NSDataDetector 检测到的电话号码