1

I have custom cell and dynamic height of label in that cell. Then I add new cell, but height of all cells is equal (the same). My code:

class MyTable: UITableViewController {
var height: CGFloat = 0.0
/* ........ */
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let row = indexPath.row
    /* make some action with objects in cell */
    let cell = tableView.dequeueReusableCellWithIdentifier("order", forIndexPath: indexPath) as! OrderData
        self.height = cell.address_lbl.frame.origin.y + cell.address_lbl.frame.height + 8
    return cell
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return self.height

}
}

I understand, that the question is duplicate, but I don't know, how to make dynamic height. P.S. that solution must work in iOS 7

  • Have a look here https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html#//apple_ref/doc/uid/TP40010853-CH25-SW1 – Matz Mar 01 '16 at 11:23
  • It doesn't work with iOS7 – Сашок Гончаренко Mar 01 '16 at 11:40
  • My fault. Best explanation is here with great samples http://stackoverflow.com/a/18746930/1518174 It's objective-C but should not be a problem to write Swift-Code – Matz Mar 01 '16 at 12:14

1 Answers1

1

It's not the problem of the code, code is doing what you have wrote. There is a problem with the logic, table view datasource methods are not called in serial order as you are expecting. You should move height calculations to heightForRowAtIndexPathto have different height for the cells.

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29