Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default UITableView Cell automatic height

i have a UITableViewController, with a cell of the style uitableviewcellstylesubtitle, where the subtitle-label has numberoflines=0

and if i do

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0

the cells dont do automatic height, can it really be true - that you can't use that on the default styled cells?

EDIT:

Simple population

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let thisHelpArticle = helpObjects[indexPath.section]

    cell.textLabel!.text = thisHelpArticle.helpHeadline
    cell.detailTextLabel!.text = thisHelpArticle.helpDescription

    return cell
}
like image 762
magnuskahr Avatar asked Apr 25 '16 19:04

magnuskahr


People also ask

How do you find the height of a cell?

Select the row or rows that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click Row Height. In the Row height box, type the value that you want, and then click OK.

How do I change cell height in Swift?

To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we'll need to make use of automatic dimension property.

What is UITableView in Swift?

A view that presents data using rows in a single column.


1 Answers

UPDATE!!! In order for the default cell subtitle to autoresize the cell, you MUST set both label's number of lines to 0. Otherwise it doesn't work. Weird.

cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0

You can auto resize default sytled cells. In its simplest form I have the following code. Which is auto resizing the default styled subtitle cell.

class ViewController: UIViewController {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44
    }

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

extension ViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        let thisHelpArticle = "Lorem ipsum dolor sit amet, vel porttitor blandit, aliquam tristique vestibulum, enim vel eros fames id, in gravida vestibulum gravida tempor, et vel libero sed mauris. Suspendisse ut placerat viverra dictum, ante ante vel ut vestibulum sollicitudin phasellus. Dictumst adipiscing adipiscing nisl, fusce ut. Ante wisi pellentesque, et aliquam rhoncus eget convallis quam voluptate, ut nec quis, sodales ullamcorper elementum pellentesque sagittis vitae, dolor justo fermentum amet risus. Eu placerat ultricies. Ipsum sodales, massa elit, in neque, sed penatibus gravida, cursus eget. Ut tincidunt at eu, wisi dis vel penatibus eget, volutpat ligula vel tortor morbi feugiat, dui et eiusmod dis sociis. Iaculis lorem molestie laoreet sit, orci commodo, fusce vestibulum sapien, quisque egestas maecenas sed rem in nisl."

        cell.textLabel?.numberOfLines = 0
        cell.detailTextLabel?.numberOfLines = 0

        cell.textLabel!.text = thisHelpArticle
        cell.detailTextLabel!.text = thisHelpArticle

        return cell
    }
}

extension ViewController: UITableViewDelegate {

}
like image 199
oyalhi Avatar answered Oct 01 '22 18:10

oyalhi