【发布时间】: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