как использовать количество статических ячеек в UITableView, используя XIB - PullRequest
0 голосов
/ 21 июня 2019

Я пытался использовать статические ячейки, используя XIB, но в ячейке для строки в пути индекса я получаю сообщение об ошибке типа «Невозможно преобразовать возвращаемое выражение типа« UITableViewCell.Type »для возврата типа« UITableViewCell »» в строке «Возврат 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
        }
    }

}

Ответы [ 3 ]

2 голосов
/ 21 июня 2019

Устранить 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)")
    }
}
0 голосов
/ 21 июня 2019
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

}

0 голосов
/ 21 июня 2019

У вас есть

Невозможно преобразовать возвращаемое выражение типа 'UITableViewCell.Type' в возвращаемый тип 'UITableViewCell

потому что вы возвращаете a cell как return UITableViewCell, что означает, что вы возвращаете целый UITableViewCell класс вместо single UITableViewCell(), следовательно, в последнее, что вам нужно вернуть 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
    }
...