UITableView cell嵌套textView

来源:互联网 发布:淘宝正品aj篮球鞋店铺 编辑:程序博客网 时间:2024/06/07 16:05

想必大家会遇到cell里添加textView输入框的需求,这遇见两个问题: 自动计算输入框的高度,以及键盘弹起不能遮盖输入框。此次项目遇见这个问题在此和大家探讨一下。(在此使用YYText)


1.注册键盘通知

//注册通知,监听键盘弹起事件

    [[NSNotificationCenterdefaultCenter]addObserver:self

                                            selector:@selector(handleKeyboardDidShow:)

                                                name:UIKeyboardWillShowNotification

                                              object:nil];

//注册通知,监听键盘消失事件

    [[NSNotificationCenterdefaultCenter]addObserver:self

                                            selector:@selector(handleKeyboardDidHidden)

                                                name:UIKeyboardWillHideNotification

                                              object:nil];


2.计算输入框的高度(写在cell里)

-(void)textViewDidChange:(YYTextView *)textView{

    _newsMainModel.value = textView.text;

    CGSize constraintSize =CGSizeMake(kScreenWidth -142,MAXFLOAT);

    //计算高度

    CGSize size = [textViewsizeThatFits:constraintSize];

    if (size.height <=30) {

        self.contentHeight =30;

    }elseif(size.height <80){

        self.contentHeight = size.height;

    }

}


- (void)setContentHeight:(float)contentHeight{

    if (_contentHeight != contentHeight) {

        _contentHeight = contentHeight;

        _textView.height = contentHeight;

       //把高度赋值给model

        _newsMainModel.inputHeight = contentHeight;

        NewMainListController *mainListCon = (NewMainListController *)self.viewController;

      //刷新cell的高度(在此不能用reloadData,会造成输入失去焦点,你可以试一下

        [mainListCon.mainTabbeginUpdates];

        [mainListCon.mainTabendUpdates];

    }

}


3.回调获取焦点的输入框写在cell里)

-(void)textViewDidBeginEditing:(YYTextView *)textView{

    if (_rowBlock) {

        _rowBlock(_indexPaths);

    }

}


- (void)handleKeyboardDidShow:(NSNotification*)paramNotification{

    //获取键盘高度

      NSValue *keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];

     CGRect keyboardRect;

     [keyboardRectAsObject getValue:&keyboardRect];

     _keyBordHeightt = keyboardRect.size.height;

     //获取当前cell在tableView的所处位置

     float contentY = [_mainTab rectForRowAtIndexPath:_indexPaths].origin.y;

        if (contentY - slidDistance +_keyBordHeight + 80 > kScreenHeight - 64) {

          if (contentY - slidDistance + 50 >= kScreenHeight - 64) {

              //判断这一页的最后一个输入框,避免有空白

             [UIView animateWithDuration:.3 animations:^{

                 _mainTab.top = -_keyBordHeight;

             }];

         }else{

             [UIView animateWithDuration:.3 animations:^{

                 _mainTab.top = -(contentY - slidDistance+_keyBordHeight - kScreenHeight + 64 +                    80);

            }];

        }

    }

}



- (void)handleKeyboardDidHidden{

    [UIView animateWithDuration:.3 animations:^{

        _mainTab.top = 0;

    }];

}


//删除通知

-(void)dealloc{

    [[NSNotificationCenterdefaultCenter]removeObserver:self];

}