Gaurav Gupta
Gaurav Gupta

Reputation: 603

UITableview Height set according to cells

I have UITableView in UIViewController in this view controller total 3 tableview or 2 UICollectionView. so in this one tableview contain many cells or cells data are not static its dynamic according to server. So, I create the table view height outlet & its set on cellforrowatindex function of tableview.

So, we use to set height tableview_height.constant=tableview_height.constant+cell.frame.size.height using this code my problem showing whitespace on the end of UITableViewCell. If I increase the cell then cell are cut from the bottom. I am giving below constraint show in the pic.

enter image description here

Upvotes: 0

Views: 1005

Answers (2)

LowKostKustomz
LowKostKustomz

Reputation: 424

Check my answer on the almost same question.

How to increase the height of parent view according to height of UITableView in swift?

It is the best way to achieve what you want, because it does not depend on your table settings or if you have header/footer/custom insets/whatever.

Hope it will help.

Upvotes: 0

Jayesh Miruliya
Jayesh Miruliya

Reputation: 3317

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

{
    if indexPath.row == 0
    {
        tableview_height.constant = 0
        tableview_height.constant=tableview_height.constant+cell.frame.size.height
    }
    else
    {
        tableview_height.constant=tableview_height.constant+cell.frame.size.height
    }

    return cell
}

Upvotes: 0

Related Questions