Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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];
}
like image 359
SAHM Avatar asked Mar 08 '15 20:03

SAHM


1 Answers

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.

like image 197
Travis M. Avatar answered Sep 29 '22 05:09

Travis M.