Dynamically change cell's height while typing text, and reload the containing tableview for resize

穿精又带淫゛_ 提交于 2019-12-22 18:22:16

问题


I'm trying to have a UITableView with several cells types. One of my cells needs to dynamically change his height while typing text in it. Here is the code for change the cell's height (in the cell's class), which works good:

func textViewDidChange(_ textView: UITextView)
{
    var newFrame:CGRect = noteTextView.frame
    newFrame.size.height = noteTextView.contentSize.height
    noteTextView.frame = newFrame
    self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.width, height: noteTextView.contentSize.height)
}

But the cell is overlaying the cell under him. Nor adding those lines on the parent tableView member helped:

mParentTableView.setNeedsLayout()
mParentTableView.layoutIfNeeded()

Any idea how to tell the tableView recalc its contained cells sizes ?

(XCode 9, Swift 4)


回答1:


Using tableView's [tableView beginUpdates] and [tableView endUpdates] will solve this issue. See this answer for more information.




回答2:


Set delegate On Your UITableViewCell like that

var textDidChanges: (() -> Void)? = nil

func textViewDidChange(_ textView: UITextView) {
    textDidChanges?()
}

and Your tableView cellForRowAt

cell.textDidChanges = {
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
} 

How to make a UITableViewCell with a UITextView inside, that dynamically adjust its height, on the basis of the UITextView?



来源:https://stackoverflow.com/questions/48851614/dynamically-change-cells-height-while-typing-text-and-reload-the-containing-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!