iOS 8 Self Sizing Cells - Allow Zero Height

寵の児 提交于 2019-12-10 11:54:31

问题


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?


回答1:


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.




回答2:


I had a similar problem some time ago. I would use dynamic cells, and remove the hidden cells in

numberOfRowsInSection

and in

cellForRowAtIndexPath

You should try to add a property, like "hidden" in your cell content array, and check when you load/reload your data. That works (of course) still fine with Autolayout.



来源:https://stackoverflow.com/questions/29365730/ios-8-self-sizing-cells-allow-zero-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!