Self sizing specific UITableView Cell

246 views Asked by At

I know how to make UITableViewCells self size in UITableView. The question is how to only a specific cell self size itself. I have four different custom UITableView Cells in my table and I want to make only one cell self size itself.

2

There are 2 answers

2
Rashwan L On BEST ANSWER

You have to know which rows that you want to implement the UITableViewAutomaticDimension for because when because when heightForRowAt is called, no cells are created yet so you can´t check the cell type ye that´s why you need to check the indexPath.row instead.

Something like this for example:

switch indexPath.row {
case 0:
    return 100
case 1,2,3:
    return UITableViewAutomaticDimension
default:
    return 100
}
2
Reinier Melian On

You can check which cell you need to return dinamicHeigth and in func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat return UITableViewAutomaticDimension according

Example code using row 0 as condition

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if(indexPath.row == 0)
        {
            return UITableViewAutomaticDimension
        }

        return 88
    }