Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

set heightForRowAt for specific UITableViewCell

Hi developers I’m stuck trying to find a way to set heightForRowAt on a UITableViewCell

this is my cellForRowAt

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

    UITableViewCell {
            
            guard let rows = rows, rows.count > indexPath.row, let row = rows[indexPath.row] as StationDetailsRowType? else { return UITableViewCell() }
            
            switch row {
                
            case .mapLocation:
                return configureMapCell(indexPath: indexPath)
                
            case .address:
                return configureStationDetailCell(indexPath: indexPath, rowType: .address)
                
            case .hours:
                return configureHourCell(indexPath: indexPath)
                
            case .phone:
                return configureStationDetailCell(indexPath: indexPath, rowType: .phone)
                
            case .fuelGrades:
                return configureFuelGradeCell(indexPath: indexPath) //this is the one I want to set height
    
            case .bpRewardsUnlocked:
                return configureStationDetailCell(indexPath: indexPath, rowType: .bpRewardsUnlocked)
                
            case .facilities:
                return configureStationDetailCell(indexPath: indexPath, rowType: .facilities)
            
            case .foodAndDrinks:
                return configureStationDetailCell(indexPath: indexPath, rowType: .foodAndDrinks)
            case .fuelGradesUS:
                return configureFuelGradeCell(indexPath: indexPath)
            }
        }

the enum looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enum StationDetailsRowType {
    
    case mapLocation
    case address
    case hours
    case phone
    case fuelGrades
    case fuelGradesUS
    case facilities
    case foodAndDrinks
    case bpRewardsUnlocked
}

what I want to achieve is when the cell indexPath goes to fuelGrades
the CGFloat is equal to 279 else return UITableView.automaticDimension

something like

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == StationDetailsRowType. fuelGrades {
            return 279
        }else {
            return UITableView.automaticDimension
        }

    }

other functions

    // this is call when the fuelGrades cellForRowAt is call and the market for US is true
            func configureTableViewFuelGrades(cellIdentifier: String = 
    
    StationDetailTableViewCell.cellID, indexPath: IndexPath) -> UITableViewCell {
                
                guard let cell = tableView.dequeueReusableCell(withIdentifier: FuelGradesTypesTableViewCell.cellID, for: indexPath) as? FuelGradesTypesTableViewCell else { return UITableViewCell() }
                let cellFillInformation = fuelGradesInformationStore
                let fuelType = cellFillInformation.flatMap({ $0.fuelGradesType })
                let fuelPrice = cellFillInformation.flatMap({ $0.fuelGradesPrice })
                
                cell.fuelGradesTypes = fuelType
                cell.fuelGradesPrice = fuelPrice
                cell.sections = cellFillInformation.count
                cell.selectionStyle = .none
                cell.layoutIfNeeded()
                return cell
            }
            
//this is basically call on the cellForRow 
            func configureFuelGradeCell(indexPath: IndexPath) -> UITableViewCell{
                if isAUS {
                    return configureFuelGradeIconCell(indexPath: indexPath)
                }
                else if isUS{
                    return configureTableViewFuelGrades(indexPath: indexPath) //here's the top function
                } else {
                    return configureStationDetailCell(indexPath: indexPath, rowType: .fuelGrades)
                }
            }
            
//I pre-set this CGFloat but can be a different row that's why I want to know exactly when to set 279 height on specific Row
            func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                if indexPath.row == 4 {
                    return 279
                }else {
                    return UITableView.automaticDimension
                }
        
            }

>Solution :

Your current attempt at heightForRowAt won’t compile since indexPath.row is not comparable against your enum.

The solution is to use the same logic you have in cellForRowAt.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if let rows = rows, rows.count > indexPath.row, let row = rows[indexPath.row] as StationDetailsRowType? {
        if row == .fuelGrades {
            return 279
        }
    }

    return UITableView.automaticDimension
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading