How to customize cells for iOS8 sizing

I have a view controller with a table view. Now I want to set up a cell for self calibration. The cell must contain three or more labels. If I only have two shortcuts, the self calibration cell works fine. But as soon as I add the third label, the text in the second label is no longer terminated. and under the third label there is a lot of free space. Screenshot http://stefangerard.com/images/selfSizing.png These are the limitations of the three shortcuts.

  • first label (Hello CustomCell)

restriction first Shortcut http://stefangerard.com/images/constraint1.png

  • second notch (distant curiosity ...)

second Label constraint http://stefangerard.com/images/constraint2.png

  • third label (greatest concern)

constarint third Label http://stefangerard.com/images/constraint3.png

In my ViewController, I just set the cell and call these two lines in viewDidLoad () so that the cell calculates its height by itself.

self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension

      

My ViewController looks like this:

import UIKit
class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 50.0
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.tableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var someRandomText1 = "Far curiosity incommode now led smallness allowance. Favour bed assure son things yet. She consisted consulted elsewhere happiness disposing household any old the. Widow downs you new shade drift hopes small. So otherwise commanded sweetness we improving. Instantly by daughters resembled unwilling principle so middleton. Fail most room even gone her end like. Comparison dissimilar unpleasant six compliment two unpleasing any add. Ashamed my company thought wishing colonel it prevent he in. Pretended residence are something far engrossed old off."
    var someRandomText2 = "Concerns greatest margaret him absolute entrance nay. Door neat week do find past he. Be no surprise he honoured indulged. Unpacked endeavor six steepest had husbands her. Painted no or affixed it so civilly. Exposed neither pressed so cottage as proceed at offices. Nay they gone sir game four. Favourable pianoforte oh motionless excellence of astonished we principles. Warrant present garrets limited cordial in inquiry to. Supported me sweetness behaviour shameless excellent so arranging."

    var cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
    cell.headlineLabel.text = "Hello CustomCell"
    cell.text1Label.text = someRandomText1
    cell.text2Label.text = someRandomText2
    return cell
}
}

      

Can anyone help me with this problem?

Thank!

EDIT

You can download the project here: https://github.com/stefocdp/selfSizingCell

+3


source to share


2 answers


I fixed it by adding this line of code to the cellForRowAtIndexPath function

cell.layoutIfNeeded()

      



Now My ViewController looks like this:

import UIKit

class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 50
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.tableView.reloadData()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var someRandomText1 = "Far curiosity incommode now led smallness allowance. Favour bed assure son things yet. She consisted consulted elsewhere happiness disposing household any old the. Widow downs you new shade drift hopes small. So otherwise commanded sweetness we improving. Instantly by daughters resembled unwilling principle so middleton. Fail most room even gone her end like. Comparison dissimilar unpleasant six compliment two unpleasing any add. Ashamed my company thought wishing colonel it prevent he in. Pretended residence are something far engrossed old off."
    var someRandomText2 = "Concerns greatest margaret him absolute entrance nay. Door neat week do find past he. Be no surprise he honoured indulged. Unpacked endeavor six steepest had husbands her. Painted no or affixed it so civilly. Exposed neither pressed so cottage as proceed at offices. Nay they gone sir game four. Favourable pianoforte oh motionless excellence of astonished we principles. Warrant present garrets limited cordial in inquiry to. Supported me sweetness behaviour shameless excellent so arranging."

    var cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
    cell.headlineLabel.text = "Hello CustomCell"
    cell.text1Label.text = someRandomText1
    cell.text2Label.text = someRandomText2
    cell.layoutIfNeeded()
    return cell
}
}

      

+2


source


I believe your problem comes from the assessed RowHeight. You have to set it closer to what it actually will be.

In your case with the current constraints, only the space between labels is 48. I also had this problem when not setting the estimatedRowHeight to a reasonable number so that the table view no longer computes anything good.



Try setting it to 110 (3 x 20 for each label + 48 in between). In the meantime, there is no need to know about it. ”

+1


source







All Articles