【问题标题】:UITableView's contentOffset keep changing itselfUITableView 的 contentOffset 不断改变自己
【发布时间】:2011-03-16 17:28:34
【问题描述】:

已更新:向下滚动查看已更新。

我有一个 UITableView,其中单元格包含几个 UITextView(s)。当用户开始编辑文本并显示键盘时,我使用 UITableView 的方法 scrollToRowAtIndexPath::: 调整 TableView 的框架和滚动编辑单元格的大小以使其可见。在用户真正开始输入之前,所有这些都可以完美运行。问题是,每次敲击键盘的键时,UITableView 的 contentOffset 属性的 y 值都会发生变化。

我尝试为每个按键记录记录

NSLog(@"%1.2f, %1.2f, %1.2f, %1.2f", 
          self.table.contentSize.width, 
          self.table.contentSize.height, 
          self.table.contentOffset.x, 
          self.table.contentOffset.y);

这就是结果

2011-03-17 00:13:05.380 ...[6243:207] 320.00, 355.00, 0.00, 8.00
2011-03-17 00:13:06.138 ...[6243:207] 320.00, 355.00, 0.00, 13.00
2011-03-17 00:13:06.848 ...[6243:207] 320.00, 355.00, 0.00, 8.00
2011-03-17 00:13:07.578 ...[6243:207] 320.00, 355.00, 0.00, 13.00
2011-03-17 00:13:08.377 ...[6243:207] 320.00, 355.00, 0.00, 8.00

如您所见,contentOffset.y 一直在变化,这使得 TableView 在每次击键时都会上下跳跃。我的代码中没有任何地方可以更改 TableView 的 contentOffset 值,或者至少没有明确更改。有人知道会发生什么吗?

这是我在显示/隐藏键盘时用于调整 TableView 大小的代码

- (void)keyboardDidShow:(NSNotification *)notification
{
    if (keyboardAlreadyShow) // in case user jump from one TextView to another
        return;

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newFrame = self.view.bounds;
    newFrame.origin.x = self.table.frame.origin.x;
    newFrame.size.width = self.table.frame.size.width;
    newFrame.size.height = keyboardTop;

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration 
                     animations:^{
                         self.table.frame = newFrame;
                     }];

    NSIndexPath *index = [table indexPathForCell:editingCell];
    [self.table scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionNone animated:YES];

    keyboardAlreadyShow = YES;

}

- (void)keyboardWillHide:(NSNotification *)notification 
{   
    CGRect newFrame;
    newFrame.origin.y = self.table.frame.origin.y;
    newFrame.origin.x = self.table.frame.origin.x;
    newFrame.size.width = self.table.frame.size.width;
    newFrame.size.height = 430;

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration 
                     animations:^{
                         self.table.frame = newFrame;

                     }];

    [self.table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

- (void)keyboardDidHide:(NSNotification *)notification 
{

    keyboardAlreadyShow = NO;

}

任何帮助将不胜感激。谢谢。

已更新:现在我想我可以将问题缩小到这一特定行

txtLatInteger.text = [NSString stringWithFormat:@"...

哪个 txtLatInteger 是 UITextView 驻留在 TableViewCell 中,并且每次另一个 TableViewCell 中的另一个 TextView 更改时都会更新。因此,如果我注释掉这一行,TableView 将不会移动。知道会发生什么吗?

UPDATED2:似乎将 UITextView 放入 UITableViewCell 会导致此弹跳问题。将输入字段更改为 UITextField 解决这个问题(有点)。但是我必须从 UITextView 中牺牲一些我需要的特定行为。 :(

【问题讨论】:

    标签: iphone keyboard tableview edit


    【解决方案1】:

    我正在处理我的键盘出现时遇到的与您的代码非常相似的弹跳问题。当我的键盘出现时,我会以与您的代码类似的方式动画调整 UITableView 的大小。我将您的代码移植到我的 keyBoardDidShow 通知中,并且获得了与以前几乎相同的功能。

    我没有看到您在输入时出现弹跳问题,因此我推测您的问题与您的 keyboardDidShow 或 keyboardDidHide 通知无关。您是否安装了一个名为 Cinch 的程序?我已经看到它导致 UITableViews 在模拟器中表现异常。

    【讨论】:

    • 感谢您的回复。不,我没有安装 Clinch 应用程序。此外,这在模拟器和真实设备上都会发生。 :(
    【解决方案2】:

    似乎将 UITextView 放入 UITableViewCell 会导致此弹跳问题。将输入字段更改为 UITextField 解决这个问题(有点)。但是我必须从 UITextView 中牺牲一些我需要的特定行为。 :(

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多