2015-10-30 83 views
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 
} 

结构:

enter image description hereenter image description here

,虽然小区的原型高度设定为44.0像素,但在cellForRowAtIndexPath方法我想和100.0 PX覆盖此值。

+0

的委托方法只是为了检查 - 你希望你的tableview细胞使用基于'UIImageView'在运行时的大小自动布局自动调整自己? –

+0

我忘了**添加图像的底部约束**,导致问题,并且没有更新方法需要:cell.layoutIfNeeded() cell.setNeedsUpdateConstraints() –

回答

0

它不会像你这样工作。

你应该实现的UITableViewDelegate

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    // calculate size of image and return its height here 
} 
相关问题