4

I've scoured through all of the questions here regarding a similar issue and I still seem to have this problem no matter what I do. I'm on iOS 8.3.

You can find the full code at: https://github.com/DanielRakh/CellHeightTest.git

I have a cell that dynamically resizes based on the content of a UILabel that varies from single to multi-line text. Everything seems to be fine initially but when I start scrolling fast and quickly switch directions

I get a warning in the debugger:

2015-03-10 02:02:00.630 CellHeight[21115:3711275] 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.

Here's how it looks in the simulator. I've marked the discrepancies in red:

enter image description here

Here's the simple setup in IB where I have a UILabel pinned top(11), bottom(11), leading(8), and trailing(8) with a custom cell at 45pts:

enter image description here

Here is my code. The cell is a custom subclass thats pretty much empty except for the IBOutlet for the label:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var arr = [String]()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        for _ in 1...10 {
            
            arr.append("This is an amount of text.")
            arr.append("This is an even larget amount of text. This should move to other lines of the label and mace the cell grow.")
            arr.append("This is an even larger amount of text. That should move to other lines of the label and make the cell grow. Crossing fingers. This is an even larger amount of text. That should move to other lines of the label and make the cell grow. Crossing fingers.")
        }
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 45.0

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        tableView.reloadData()
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TstTableViewCell
        cell.tstLabel.text = arr[indexPath.row]
        return cell
    }

}

EDIT: I've found that if I created the cells programmatically and set the constraints programmatically everything seems to work as intended. Weird. This seems to be an IB + AL issue.

DanielRak
  • 239
  • 1
  • 2
  • 11
  • 1
    I've seen more than a few instances where self-sizing cells (introduced in iOS 8) are still buggy and don't end up working correctly. If I had to guess, in this case the problem may be due to the `preferredMaxLayoutWidth` on each UILabel not being set correctly automatically, which is supposed to happen. That property instructs the label when to word wrap, and thus informs how tall it will be. Take a look at this discussion to see more specifics on how you could fix this: https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/7 – smileyborg Mar 10 '15 at 14:51
  • 1
    If that doesn't work, you may need to revert back to the iOS 7 compatible mechanism for sizing each row (until self sizing cells work better). The details for that are here: http://stackoverflow.com/questions/18746929 – smileyborg Mar 10 '15 at 14:52

0 Answers0