我在TableView中使用TextView,其高度必须根据内容增加。 我是这样做的
{{1}}
TableView有静态单元格。已正确地为TextView提供了约束。我在这里主要关心的是textView的委托没有被调用。我添加了断点来检查它。
答案 0 :(得分:1)
使用代码手动设置UITextView的文本时,不会调用textViewDidChange:方法。 (如果您设置了文本视图的委托,则会在用户编辑它时调用它。)
一种可能的解决方法是手动调用textViewDidChange:随时编辑文本。例如:
textView.delegate = self
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
self.textViewDidChange(self.textView)
这种做法的一种hackish方式,但它完成了工作
希望这对你有所帮助。
修改强>
试试这个为我工作
class TableViewController: UITableViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 70
tableView.rowHeight = UITableViewAutomaticDimension
textView.delegate = self
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
self.textViewDidChange(self.textView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.item == 1{//****Or index of your textView****
return textView.contentSize.height;
}
return 70
}
}
extension TableViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
tableView.reloadData()
}
}
<强>更新强>
class TableViewController: UITableViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 70
tableView.rowHeight = UITableViewAutomaticDimension
textView.delegate = self
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
self.textViewDidChange(self.textView)
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.item == 0{//****Or index of your textView****
return textView.contentSize.height;
}
return 70
}
}
extension TableViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
}
func textViewDidEndEditing(_ textView: UITextView) {
tableView.reloadData()
}
}