tzxdtc10
tzxdtc10

Reputation: 29

Tableview cell height as per content in it

Using swift5 i tried below code but unable to make it a adjustable height of cell. Any one can help me in that?

    tableView.rowHeight = tableView.frame.height/8
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        tableView.estimatedRowHeight = 100
        return UITableView.automaticDimension
    }

Now enter image description here

what I want

enter image description here

View Architecture

enter image description here

Cell Architecture enter image description here

Upvotes: 0

Views: 106

Answers (1)

mahan
mahan

Reputation: 14935

Auto height

You need to confirm to UITableViewDelegate and then call these two methods.

func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

 func tableView(_ tableView: UITableView,  estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

Or

tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 100

But in both cases, it is required the views in cell have both bottom and top constraints. Read the accepted answer of this question for more info.

More info.

UITableViewDelegate

Configuring Cell Height and Layout

Upvotes: 1

Related Questions