我正在使用 UITableView 处理提要页面,并且我正在尝试设置单元格的动态高度(以根据内容进行调整。)
我已经为单元格创建了一个自定义类,代码如下:
class cellTableViewCell: UITableViewCell, UINavigationControllerDelegate {
//OUTLETS
@IBOutlet weak var usernameLbl: UILabel!
@IBOutlet var profileImg: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
usernameLbl.text = "Username"
usernameLbl.textColor = UIColor.grayColor()
profileImg.layer.cornerRadius = profileImg.frame.size.width/2
profileImg.layer.masksToBounds = true
profileImg.addConstraint(NSLayoutConstraint(item: profileImg, attribute: NSLayoutAttribute.Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 100.0))
contentView.addSubview(profileImg)
contentView.addConstraint(NSLayoutConstraint(item: profileImg, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 100.0))
}
然后,在控制 TableView 的 View Controller 的 ViewDidLoad() 方法中,我添加了以下内容:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 160.0
但是,单元格根本没有适应,因为高度仍然小于内容。
最佳答案
我认为您的约束是完全正确的,但问题是您无法使用每个单元格具有不同高度的动态 TableViewCells 构建 UITableView。我自己尝试过,但在动态 UITableViewCell 中,tableView 的属性 rowHeight (tableView.rowHeight) 。因此,此 rowHeight 将覆盖由约束定义的单元格的每个不同高度。
关于ios - 编码的自动布局对于 UITableView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32071364/