0

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

B25Dec
  • 2,301
  • 5
  • 31
  • 54
tzxdtc10
  • 29
  • 7
  • You are looking for [this](https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview) method, that's where you should return `tableHeight / 8` – Savca Marin Feb 25 '20 at 08:31
  • If you are looking for dynamic height of the tableview, you can assign `tableView.estimatedRowHeight = 100 // you cell's height` `tableView.rowHeight = UITableView.automaticDimension` and also you can take height constraint of tableview and write ` self.tblHConstraint.constant = self.tblOfferView.contentSize.height` in `willDisplay cell` method – Niraj Feb 25 '20 at 08:39

1 Answers1

1

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

mahan
  • 12,366
  • 5
  • 48
  • 83
  • Thank you for ur answer.I've tried the answer but the cell height only changed by _return 100_...and I added the cell architect I think added the constraint already.where did I miss???is's too tricky...@mahan – tzxdtc10 Feb 25 '20 at 09:21
  • @tzxdtc10 I do not know if it is tricky for you. Do you need to have auto height or fixed height (table.frame.height/8) ? `print(table.frame.height)` in one of the above methods should you have fixed height. I think it is either `0` or `nill`. – mahan Feb 25 '20 at 09:41
  • @tzxdtc10 I updated the asnwere. There was a problem. Return `return UITableView.automaticDimension` in `heightForRowAt` method and `100` in `estimatedHeightForRowAt` – mahan Feb 25 '20 at 11:17
  • Thank you for ur answer but it did not work for me .Finally, I change the tableView to UIView. – tzxdtc10 Feb 27 '20 at 02:04