-1

I have a table view with a table view cell. I'm trying to implement a table view with multiple cell types. When I do, I get the following error

Could not cast value of type 'UITableViewCell' (0x11e159aa0) to 'test.CourseItemTableViewCell'

if (indexPath.section == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CourseItemTableViewCell

            cell.refreshUI()
            
            cell.cellIndex = indexPath
            cell.dataSource = self
            cell.delegate = self
            cell.backgroundColor = UIColor.clear
            cell.collectionView.reloadData()
            return cell;
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BookItemTableViewCell

            cell.refreshUI()
            
            cell.cellIndex = indexPath
            cell.dataSource = self
            cell.delegate = self
            cell.backgroundColor = UIColor.clear
            cell.collectionView.reloadData()
            return cell;
            
        }

How can I implement multiple cell types in the table view? I want one row to have one cell type and the other to be of another cell type.

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165
  • the tableview cell with identifier cell can/should only be of one type, you should use bookCell and courseCell identifiers to distinguish them – Scriptable Sep 01 '20 at 15:01
  • do you have examples of both cell types defined in your storyboard? – Russell Sep 01 '20 at 15:02
  • 1
    Does this answer your question? [Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'](https://stackoverflow.com/questions/29812168/could-not-cast-value-of-type-uitableviewcell-to-appname-customcellname) – pkamb Sep 03 '20 at 13:21

1 Answers1

0

Maybe it's that you haven't registered both cells. Here's something I have to show you what I'm talking about:

I have a UITableView that dynamically displays any of three types of cells... NSNumber, CIColor, and (not shown in this code) NSVector. Depending on the custom cells, you have pairs of labels and sliders - 1, 3, and 2 respectively.

Restricting this to NSNumber (one slider) and CIColor (three sliders), with properly working subclassed UITableCells, all I needed to do to render these two are:

  1. Properly register both cell types in viewDidLoad. Please note, this is what I think you are missing:

    tblAttributes.register(NSNumberCell.self, forCellReuseIdentifier: NSNumberCell.identifier)
    tblAttributes.register(CIColorCell.self, forCellReuseIdentifier: CIColorCell.identifier)
    
  2. Detect through code which cell type you want to render in tableView(cellForRowAt:). Note, my code should never return a UITableViewCell:

    switch attributes[indexPath.row].attributeClass {
    case "NSNumber":
        let cell = tableView.dequeueReusableCell(withIdentifier: NSNumberCell.identifier) as! NSNumberCell
        // [populate label and slider defaults here]
        return cell
    case "CIColor":
        let cell = tableView.dequeueReusableCell(withIdentifier: CIColorCell.identifier) as! CIColorCell
        // [populate label and slider defaults here]
        return cell
    default:
        // this should never happen
        return UITableViewCell()
    }
    

All told, it seems that CourseItemTableViewCell and BookItemTableViewCell are not uniquely identified.

And here's how I subclassed my cells:

class NSNumberCell:UITableViewCell {
    static let identifier = String(describing: NSNumberCell.self)

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
        // [custom code here]
    }
}

Same thing for the other custom cells.

Nimantha
  • 6,405
  • 6
  • 28
  • 69