Search code examples
xcodeuitableviewswiftios8cell

iOS 8 Self Sizing Cells - Allow Zero Height


I am using the self sizing cell feature and it works well until I want to hide a cell completely. I moved away from heightForRowAtIndexPath for this and I setup the following:

override func viewDidLoad() {
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 0
}

However when I have no text for a tableviewcell to render I get the following message:

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

I really just need a way to hide / show content dynamically. I am using a static tableview for this, so maybe I am approaching this wrong?


Solution

  • I found the solution.

    First, for a static table to use self sizing correctly, you can only have one label per table cell. I was trying to put in a lot of content into a cell and only the first label would size the cell. I could be wrong about the rule of one label per cell, and the problem might be constraints / auto layout related. I've watched the WWDC video on this, and the way I had it setup, should have worked with my existing constraints, as they where set to the contentView of the cell.

    Secondly, the UI updates needed to be coupled with begin and end rules, and a reload.

    tableView.beginUpdates()
    //-- You Table UI changes
    tableView.reloadData()
    tableView.endUpdates()
    

    You can also replace reloadData with reloadRowsAtIndexPaths to be specific to the rows to update, but my instance required all rows to be updated.