Self-Sizing (Dynamic Height) Cells in iOS 8 - Possible without Custom UITableViewCell?
Asked Answered
C

1

9

Is it possible to self-size a UITableViewCell in iOS 8 without creating a Custom UITableViewCell?

I thought that the standard UITableViewCell types (UITableViewCellStyleDefault, UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, UITableViewCellStyleValue2) had built in auto layout constraints. This is confirmed by the fact that the constraints for non-custom cells cannot be changed in Storyboard.

But when I use a non-custom cell of type UITableViewCellStyleValue1, set it up in Storyboard, set numberOfRows for textLabel and detailTextLabel to 0, and set the viewDidLoad code as below, only the textLabel of the cell is accounted for in the autosizing of the height of the cell. If the detailTextLabel ends up displaying on more lines than the textLabel, the text for detailTextLabel spills out over the top and bottom edges of the cell. Again, the cell does resize properly for the textLabel, but seems to ignore the detailTextLabel in its resizing process.

The main thing I need to know is - do I need to create a custom cell even for the rows that can use a standard cell if I want to properly support Dynamic Text and Self-Sizing?

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
}
Cannelloni answered 8/3, 2015 at 20:6 Comment(4)
Everything I've found says you have to use either -tableView:heightForRowAtIndexPath: or a custom subclass. I can't see why autolayout in default cell classes wouldn't set up properly for iOS 8 deployment, but it looks like it isn't.Riches
@Riches Has this changed in iOS 9 at all? Pulling my hair out trying to recreate all of the many and varied aspects of the right detail cell, just so that the row height will increase when the detailTextLabel is longer than 1 line. Has this been done anywhere? Trying to recreate the wheel, but there are so many things to think of that this feels like a big waste of time.Cannelloni
Lost hours to this issue -_-Godewyn
@Godewyn still facing issues surrounding this. Had hoped there would be a fix in iOS 9.Cannelloni
H
4

I just tried this in iOS 10/XCode 8 (same results in iOS 9/XCode 7) with the different cell types and it looks like it's possible ONLY for the textLabel and not for the detailTextLabel.

(basically repeating the issue that the OP mentioned)

ViewController code that sets some text alternately on detailTextLabel and textLabel.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        if indexPath.row % 2 == 0 {
            cell.textLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            cell.detailTextLabel?.text = "<- textLabel"
        } else {
            cell.textLabel?.text = "detailTextLabel ->"
            cell.detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
        return cell
    }

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

}

Make sure you set the textLabel and textDetailLabel's line property to 0 and here are the results.

Basic Cell enter image description here

Right Detail Cell enter image description here

Left Detail Cell enter image description here

Subtitle Cell enter image description here

I'll report this as a bug.

Hopper answered 18/7, 2016 at 21:56 Comment(2)
Did you try this for UITableViewCellStyleValue1 and the detail text label?Cannelloni
You're right, it looked like this ONLY works on the textLabel and doesn't on the detailTextLabel. This must be an issue where constraints are not being added properly to the detailTextLabel. Will update my answer.Hopper

© 2022 - 2024 — McMap. All rights reserved.