0
我在UITableViewCell
上有一个imageView,我想将cell/imageView的高度设置为不同的值,比较我在原型单元格中设置的值。我不想执行heightForRowAtIndexPath
方法,因为我不想预先计算内容的高度。如果UITableViewAutomaticDimension已设置,并且至少iOS 8 heightForRowAtIndexPath
不需要实现。原型使用UIImageView调整单元格高度
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
let iv = cell.viewWithTag(1) as! UIImageView
iv.image = UIImage(named: "img1")
let cs = iv.constraints.first! as NSLayoutConstraint
cs.constant = 100
cell.layoutIfNeeded()
cell.setNeedsUpdateConstraints()
return cell
}
结构:
,虽然小区的原型高度设定为44.0像素,但在cellForRowAtIndexPath
方法我想和100.0 PX覆盖此值。
的委托方法只是为了检查 - 你希望你的tableview细胞使用基于'UIImageView'在运行时的大小自动布局自动调整自己? –
我忘了**添加图像的底部约束**,导致问题,并且没有更新方法需要:cell.layoutIfNeeded() cell.setNeedsUpdateConstraints() –