UITableView的contentOffset不断变化
已更新:向下滚动查看已更新。
我有一个 UITableView,其中单元格包含很少的 UITextView。当用户开始编辑文本并且键盘显示时,我使用 UITableView 的方法 rollToRowAtIndexPath::: 调整 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 是驻留在 TableViewCell 中的 UITextView,并且每次另一个 TableViewCell 中的另一个 TextView 更改时都会更新。因此,如果我注释掉这一行,TableView 将不会移动。知道会发生什么吗?
更新2:似乎将 UITextView 放入 UITableViewCell 会导致此弹跳问题。将输入字段更改为 UITextField 可以解决此问题(某种程度上)。但我必须牺牲 UITextView 所需的一些特定行为。 :(
UPDATED: Scroll down for updated.
I have a UITableView which cells contain few UITextView(s). When user start editing text and keyboard show up, I resize TableView's frame and scroll editing cell to visible with method scrollToRowAtIndexPath::: of UITableView. All this work perfectly until user actually start typing. Problem is, every time the keyboard's key was hit, the y value of contentOffset property of UITableView change itself.
I try to log this for every keys stroke
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);
And this is the result
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
As you can see, the contentOffset.y just keep changing all the time, and that makes TableView jump up and down for every key stroke. There's no where in my code that change TableView's contentOffset value, or at least not explicitly. Anyone have any idea what happen?
Here's the code I use to adjust TableView size when show/hide keyboard
- (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;
}
Any help would be really appreciated. Thank you.
UPDATED: Now I think I can narrow down the problem to this specific line
txtLatInteger.text = [NSString stringWithFormat:@"...
Which txtLatInteger is an UITextView reside in the TableViewCell and will be updated every time another TextView in another TableViewCell changed. So if I comment out this line the TableView won't move. Any idea what happen?
UPDATED2: Seem like putting UITextView into UITableViewCell cause this bouncing problem. Change input field to UITextField solve this (sort of). But I have to sacrifice some specific behavior I need from UITextView. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在解决键盘出现时遇到的弹跳问题,该问题与您的代码非常相似。当我的键盘出现时,我以与代码类似的方式动画调整 UITableView 的大小。我将您的代码移植到我的 keyBoardDidShow 通知中,并且获得了与以前几乎相同的功能。
我没有看到您在打字时弹跳的问题,因此我推测您的问题不在于您的keyboardDidShow 或keyboardDidHide 通知。您是否安装了名为 Cinch 的程序?我已经看到它导致 UITableViews 在模拟器中表现奇怪。
I'm working on a bouncing issue I have when my keyboard appears that is very similar to your code. When my keyboard appears I animate the resizing of my UITableView in a similar way to your code. I ported in your code to my keyBoardDidShow notification and I got nearly the same functionality as before.
I do not see your bouncing while typing issue so I'm theorizing your problem is not with your keyboardDidShow or keyboardDidHide notifications. Do you have a program called Cinch installed? I've seen it cause UITableViews to behave strangely in the simulator.
似乎将 UITextView 放入 UITableViewCell 会导致此弹跳问题。将输入字段更改为 UITextField 可以解决此问题(某种程度上)。但我必须牺牲 UITextView 所需的一些特定行为。 :(
Seem like putting UITextView into UITableViewCell cause this bouncing problem. Change input field to UITextField solve this (sort of). But I have to sacrifice some specific behavior I need from UITextView. :(