我把UITableViewCell放在xib文件中
而且,我想根据放在Cell中的UITextView的高度来改变Cell的高度
我发现了以下文章
http://candycode.io/self-sizing-uitextview-in-a-uitableview-using-auto-layout-like-reminders-app/
我认为这是按照这篇文章进行的
但TableViewCell不会根据TextView的高度而改变
请告诉我哪里出错了。
我将详细介绍我在下面所做的事情。
1.我创建了UITableView和Cell(Xib),如下所示
TableView.swift
import UIKit
class TableView: UIView, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
@IBOutlet weak var newImage: UIImageView!
//etc...
class func instance() -> TableView {
return UINib(nibName: "TableView", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! TableView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = newTableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCellTableViewCell
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64
}
}
Cell.swift
import UIKit
class NewCellTableViewCell: UITableViewCell {
let bottomBorder = CALayer()
let vArchColor = archColor()
@IBOutlet weak var textView: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
bottomBorder.frame = CGRect(x: 20, y: self.frame.size.height - 1.0, width: self.frame.width*2, height: 1.0)
bottomBorder.backgroundColor = vArchColor.black01.cgColor
self.layer.addSublayer(bottomBorder)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
print("Selected")
}
}
我在UITableView中设置了几个必要的值。
// func textViewDidChange(_ textView:UITextView){ newTableView.beginUpdates() newTableView.endUpdates() }
TableView.swift中的 //
func initSomething(){
newTableView.rowHeight = UITableViewAutomaticDimension
newTableView.estimatedRowHeight = 10000
}
我在Cell(XibFile)中使用了Storyboard并设置了约束
当然,我也取消选中了scrollenabled。
Cell.xib的图像
答案 0 :(得分:0)
您已为heightForRowAtIndexPath:
提供了一个实现,该实现将在每行基础上覆盖您在tableView.rowHeight = ...
中设置的任何常量值。从TableViewController中删除此方法,如果文本视图的约束不明确,则应该是金色的。确保它固定在四个方面和有一个高度限制。