Expandable table view cell using auto layout is not working?

203 views Asked by At

I tried to replicate the expandable tableview cells using this tutorial

I will show what I have done and point out what am I missing here?!! I'm stuck here. It would be helpful if you point out the step I miss!!

TableviewController

class ExpandableCell: UITableViewController {

let titles = ["one" , "two"]    

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 125
//  tableView.tableFooterView = UIView(frame: CGRectZero)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return titles.count
}

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

    cell.title.text = titles[indexPath.row]
    cell.indexPath = indexPath


    switch expandedIndexPath {
    case .Some(let expandedIndexPath) where expandedIndexPath == indexPath:
        cell.showsDetails = true
    default:
        cell.showsDetails = false
    }

    return cell

}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: false)

    switch expandedIndexPath {
    case .Some(_) where expandedIndexPath == indexPath:
        expandedIndexPath = nil
    case .Some(let expandedIndex) where expandedIndex != indexPath:
        expandedIndexPath = nil
        self.tableView(tableView, didSelectRowAtIndexPath: indexPath)
    default:
        expandedIndexPath = indexPath
    }

}

var expandedIndexPath: NSIndexPath? {
    didSet {
        switch expandedIndexPath {
        case .Some(let index):
            tableView.reloadRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Automatic)
        case .None:
            tableView.reloadRowsAtIndexPaths([oldValue!], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
  }
}

TableViewCell

class TableCell: UITableViewCell {

@IBOutlet weak var title: UILabel!
@IBOutlet weak var descriptionConstraint: NSLayoutConstraint!
@IBOutlet weak var descriptionNew: UILabel!

//    let detailViewDefaultHeight: CGFloat = 44
let lowLayoutPriority: Float = 250
let highLayoutPriority: Float = 999


var showsDetails = false {
    didSet {
        descriptionConstraint.priority = showsDetails ? lowLayoutPriority : highLayoutPriority
    }
}

var indexPath = NSIndexPath()

override func awakeFromNib() {
    super.awakeFromNib()
    descriptionConstraint.constant = 0
  }
}

StoryBoard

enter image description here

This is the output I'm getting which is not the one I'm trying to get,

enter image description here

2

There are 2 answers

1
UditS On

Seems like you have set the low and high priority values in the reverse order

var showsDetails = false {
    didSet {
        descriptionConstraint.constant = showsDetails ? highLayoutPriority: lowLayoutPriority 
    }
}
1
Mitul Marsoniya On

Base on your code i think are you missing bellowing method :-

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        return UITableViewAutomaticDimension
    }