简体   繁体   中英

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在此处输入图片说明

what I want

在此处输入图片说明

View Architecture

在此处输入图片说明

Cell Architecture在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM