如何使用 XIB 在 UITableView 中使用静态单元格的数量

how to use number of static cells in UITableView using XIB's

我一直在尝试使用 XIB 的静态单元格,但是在索引路径的行单元格中,我在“return UITableViewCell”行收到类似 "Cannot convert return expression of type 'UITableViewCell.Type' to return type 'UITableViewCell'" 的错误。可以吗?一个帮我做这个,提前谢谢。

extension TriipDetailsViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0
        {
         let cell  :  TripDriverDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath) as! TripDriverDetailsCell
            return cell
        }else if indexPath.row == 1 {
             let cell  :  TripFareDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath) as! TripFareDetailsCell
             return cell
        }else if indexPath.row == 2 {
             let cell  :  TripPaymentModeCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath) as! TripPaymentModeCell
             return cell
        }
        return UITableViewCell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0{
            return 230

        }else if indexPath.row == 1{
            return 200

        }else if indexPath.row == 2{
            return 120
        }
    }

}

消除return UITableViewCell

有些人可能会说 return UITableViewCell(),但我宁愿将这种情况视为真正的错误,例如

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    case 0:
        return tableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath)

    case 1:
        return tableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath)

    case 2:
        return tableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath)

    default:
        fatalError("Unexpected row number \(indexPath.row)")
    }
}

heightForRowAt 应该类似地处理 indexPath.row 不是 0、1 或 2 的情况,例如:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch indexPath.row {
    case 0: return 230
    case 1: return 200
    case 2: return 120
    default: fatalError("Unexpected row number \(indexPath.row)")
    }
}

你有

Cannot convert return expression of type 'UITableViewCell.Type' to return type 'UITableViewCell

因为你 return 一个 cell as return UITableViewCell 那意味着你 return 一个整体 UITableViewCell class 而不是 single UITableViewCell() 因此最后你要 return cell 作为 return UITableViewCell()

  • 例如
extension TriipDetailsViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0
        {
         let cell  :  TripDriverDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath) as! TripDriverDetailsCell
            return cell
        }else if indexPath.row == 1 {
             let cell  :  TripFareDetailsCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath) as! TripFareDetailsCell
             return cell
        }else if indexPath.row == 2 {
             let cell  :  TripPaymentModeCell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath) as! TripPaymentModeCell
             return cell
        }
        return UITableViewCell() //Here you've to change
    }
let cell : UITableViewCell
   if indexPath.row == 0
{
    cell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripDriverDetailsCell", for: indexPath) as! TripDriverDetailsCell
}else if indexPath.row == 1 {
    cell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripFareDetailsCell", for: indexPath) as! TripFareDetailsCell
}else if indexPath.row == 2 {
    cell = tripDetailsTableView.dequeueReusableCell(withIdentifier: "TripPaymentModeCell", for: indexPath) as! TripPaymentModeCell
}else {
    cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
}
return cell

}