【问题标题】:Expand uitableview cell height while typing and avoid keyboard键入时扩展uitableview单元格高度并避免使用键盘
【发布时间】:2018-10-16 06:15:34
【问题描述】:

我有一个带有 textview 的 tableview。我想在键入时根据 uitextview 的内容设置 tableviewcell 高度,如果键盘隐藏了 uitexview 滚动表。

下面是根据内容扩展单元格高度的代码

func updateCellHeight(indexPath: NSIndexPath, comment: String,textview: UITextView) {

    let tempDict = arrayTexts[indexPath.row] as! NSMutableDictionary
    tempDict.setValue(comment, forKey: "content")
    arrayTexts[indexPath.row] = tempDict
    self.tblFields.beginUpdates()
    self.tblFields.endUpdates()


}

上面的代码可以工作,但它不能避免键盘

下面的代码是为了避免键盘

func textviewBeginEditing(textview: UITextView) {
    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)

    self.tblFields.contentInset = contentInsets
    self.tblFields.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    var aRect = self.view.frame
    aRect.size.height -= keyboardHeight

    if !aRect.contains(textview.frame.origin) {
        self.tblFields.scrollRectToVisible(textview.frame, animated: false)
    }

}

上面的代码有时会避开键盘,有时不会,但表格视图在键入时不断向上滚动并且无法正常工作。我的错误是什么?

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    从 textviewBeginEditing 函数中删除代码并添加以下代码

    func updateCellHeight(indexPath: NSIndexPath, comment: String, textview: UITextView) {
    
        let tempDict = arrayTexts[indexPath.row] as! NSMutableDictionary
        tempDict.setValue(comment, forKey: "value")
        arrayTexts[indexPath.row] = tempDict
    
        if textview.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height != textview.frame.size.height {
    
            UIView.setAnimationsEnabled(false)
            self.tblFields.beginUpdates()
            self.tblFields.endUpdates()
            UIView.setAnimationsEnabled(true)
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多