Gabriel Goncalves
Gabriel Goncalves

Reputation: 5160

Is it possible to change static cell height in iOS 7 using Swift?

I know it is a question that somebody maybe answered but I can't find the solution yet.

I'm trying to change the hight of a static cell by code using Swift. I'm using this method func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

But the cell size is not changing based on the content of the label. This is what I have:

@IBOutlet weak var testDetailsLabel: UILabel!
@IBOutlet weak var testDetailsCell: TestDetailsTableViewCell!

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if indexPath.section == 1 && indexPath.row == 0 {
        self.testDetailsCell.descriptionLabel?.text = self.test?.details
        self.testDetailsCell.setNeedsLayout()
        self.testDetailsCell.layoutIfNeeded()
        self.testDetailsCell.bounds = CGRectMake(0.0, 0.0, tableView.bounds.width, self.testDetailsCell.bounds.height)
        self.testDetailsCell.setNeedsLayout()
        self.testDetailsCell.layoutIfNeeded()

        let size = self.testDetailsCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) as CGSize!

        return size.height + 1
    }

    return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}

Any body know if it possible to change the height size for static cells?

Maybe the constraints are the problem? I have both sides constraints, top, bottom and height >= constraint.

Upvotes: 0

Views: 797

Answers (1)

Mister Orko
Mister Orko

Reputation: 193

I wrestled with this only last night also. I am developing in iOS8 so I apologise if this does not work in ios 7 but all I did was connect an IBOutlet for the table itself and used the following code in my viewDidLoad() method:

@IBOutlet var logInTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        logInTable.rowHeight = 50  //Make table cells fixed height

}

I hope this helps.

Thanks

Upvotes: 1

Related Questions