Dynamic height of custom cell in UITableView

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

Upvotes: 1

Views: 272

Answers (1)

Fahri Azimov
Fahri Azimov

Reputation: 11770

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.

Upvotes: 1

Related Questions