简体   繁体   中英

Dynamic table view cell, change of subview height programmatically

I have a UITableViewCell with a custom view cell. In this view cell, I have a simple UIView called imgWrapper where I added constraints as follows:

  • width = 50
  • height = 50
  • leading to superview = 20
  • top to superview = 20
  • bottom to superview = 20

Those are the only constraints in there. And I left the hugging and compression to the default ones.

In my code I've set this:

  override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 90
}

Then in in my rowAtIndex...I have this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: LogCustomCell = tableView.dequeueReusableCellWithIdentifier("logCustomCell", forIndexPath: indexPath) as! LogCustomCell

     var imgWrapperHeight: CGFloat = log.big ? 100 : 50

     cell.imgWrapperHeight.frame.size.height = imgWrapperHeight 
     return cell
}

Once I compile and run it. All the cells are the same size.

Notes:

  • I checked if log.big was true/false and it does change.
  • I've also tried to do CGRect(x,y,width,height) but also didn't work.
  • I know I can do heightForRowAtIndexPath but I want to do animations and I know we can do something like this for labels (see printscreen) which makes the tableView know the height without defining it:

在此处输入图片说明

Any ideas what I'm doing wrong?

You need to put your height logic into the UITableViewDelegate method called tableView:heightForRowAtIndexPath , something like the following:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return (true ? 100 : 50) + 2 * 20
}

PS. I've written this in Swift 2, thus the override keyword.

Delegate method "heightForRowAtIndexPath" will do it for you. You can know cell index for which you are returning height from indexPath.row and hence return height accordingly.

ie

if indexPath.row == 0
{
  return 70
}
else if indexPath.row == 1
{
  return 100
}

add

self.automaticallyAdjustsScrollViewInsets = false

before these two lines

tableView.estimatedRowHeight = 50
tableView.cellHeight = UITableViewCellAutomaticDimension

Solutions 1:

As mentioned by the users. You can set the row height in your UITableViewController like so:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return (true ? 100 : 50) + 2 * 20
}

Solution 2:

Set the height constraint on the element that will determine the height of cell. Then create an outlet in your VC for NSLayoutConstraint While you are setting the content of the cell, you can do:

Ex: @IBOutlet var imgWrapperHeightConstraint: NSLayoutConstraint!

...

override func tableView(tableView: UITableView, cellAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    imgWrapperHeightConstraint.constant = 50 // Or whatever value you want
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM