0

我在自定义 UITableViewCell 中有一个 UiTextView。当我选择一个文本视图时出现键盘。

在此处输入图像描述

但有时会出现奇怪的行为:

在此处输入图像描述

键盘出现后,选定的 tableviewCell 未完全显示。有任何想法吗?我花了2天时间解决了这个问题,请帮忙。

4

2 回答 2

1

您可以在显示键盘时向上移动视图

//Add to viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];

//Add to View Controller

//Pushes the view up if one of the table forms is selected for editing
- (void) keyboardDidShow:(NSNotification *)aNotification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
    [UIView commitAnimations];
    isRaised = [NSNumber numberWithBool:YES];
}

//Pushes view back down
- (void) keyboardDidHide:(NSNotification *)aNotification
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y+moveAmount);
    [UIView commitAnimations];
    isRaised = [NSNumber numberWithBool:NO];
}
于 2012-06-28T18:03:08.007 回答
0

总之,我通过观察 tableView.ContentOffset 值解决了这个问题。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (_isScrollingDownLocked) {
        CGPoint newPoint = (CGPoint)[[change valueForKey:NSKeyValueChangeNewKey ] CGPointValue];
        CGPoint oldPoint = (CGPoint)[[change objectForKey:NSKeyValueChangeOldKey] CGPointValue];
        if (newPoint.y<oldPoint.y) {
            CGPoint point = CGPointMake(oldPoint.x, oldPoint.y);
            [self removeObserver:self forKeyPath:@"self.tableView.contentOffset"];
            self.tableView.contentOffset = point;
            [self addObserver:self forKeyPath:@"self.tableView.contentOffset"    options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        }
    }
}
于 2012-06-30T15:55:39.647 回答