swift 为特定的UITableViewCell设置行高度

c7rzv4ha  于 2023-03-07  发布在  Swift
关注(0)|答案(1)|浏览(184)

嗨,开发人员,我一直在寻找一种方法来设置UITableViewCell上的heightForRowAt
这是我的手机号

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)
            }
        }

枚举如下所示:

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

我想要实现的是当单元格indexPath转到燃料等级时CGFloat等于279,否则返回UITableView.automaticDimension
比如

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

    }

其他职能

// 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
                }
        
            }
2guxujil

2guxujil1#

您当前对heightForRowAt的尝试无法编译,因为indexPath.row与您的enum不可比。
解决方案是使用与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
}

相关问题