Search code examples
iosswift4.2

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


I have been trying to use static cells using XIB's ,But in cell for row at index path I am getting an error like "Cannot convert return expression of type 'UITableViewCell.Type' to return type 'UITableViewCell'" at the line of " return UITableViewCell".Can any one help me to do this ,Thanks in advance.

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

}

Solution

  • 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
    

    }