【发布时间】:2015-10-30 14:59:40
【问题描述】:
我在UITableViewCell 上有一个 imageView,我想将单元格/imageView 的高度设置为不同的值,比较我在原型单元格中设置的值。我不想实现heightForRowAtIndexPath 方法,因为我不想预先计算内容的高度。如果设置了 UITableViewAutomaticDimension 并且至少在 iOS 8 上heightForRowAtIndexPath 不需要实现。
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 px,但在cellForRowAtIndexPath 方法中我想用 100.0 px 覆盖这个值。
【问题讨论】:
-
只是为了检查 - 您希望您的表格视图单元格在运行时根据
UIImageView的大小使用自动布局自动调整自己的大小? -
我忘记添加图像的底部约束,导致问题,并且不需要任何更新方法:cell.layoutIfNeeded() cell.setNeedsUpdateConstraints()
标签: ios uitableview uiimageview