1

I'm currently following along with a guide (creating custom tableview cells in swift) to create a custom cell for a UITableView in Swift. However, I'm having an error when I run the project. Below is the code where the error is triggered:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //  ***** Error triggers on the line below ******
    let cell:MyCustomCellModel = self.plantList.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCellModel

    cell.plantImage.backgroundColor = self.colors[indexPath.row]
    cell.plantLabel.text = self.animals[indexPath.row]

    return cell
} 

My issue is identical to ones described in other stackoverflow posts (namely: Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'), however none of the solutions therein were able to fix my problem.

Here are the various attempts I've made to fix the problem.

  • Checked that the cell has the correct class and identifier. I checked this first, and below are two relevant screenshots. Class Name Identifier

  • I have tried both of the below code solutions to register my class(which I put into the viewdidload method), with neither providing a fix:

    self.plantList.register(MyCustomCellModel.self, forCellReuseIdentifier: "cell") self.plantList.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

  • I have also tried many of the little fixes that worked for people mentioned in the various stackoverflow posts on this issue, but none of them have worked for me.

Any tips would be greatly appreciated. If any other source code is needed to debug this, I will gladly post it.

Su_toL
  • 167
  • 2
  • 15
  • `self.plantList.dequeueReusableCell...` vs `tableView.dequeueReusableCell...` Could you check if `self.plantList` is indeed the `tableView` parameter of that method? Else, try to call also `func dequeueReusableCell(withIdentifier:, for:)`, (with IndexPath). – Larme Apr 12 '17 at 16:38
  • Yep, self.plantList is the tableView parameter. I've tried using the func with ', for: indexPath' and that gave me the same error. – Su_toL Apr 12 '17 at 16:44
  • Is the cell id the same in your storyboard and 'cellReuseIdentifier'? – Mat Apr 12 '17 at 17:04
  • Yep! The line of code is: let cellReuseIdentifier = "cell" – Su_toL Apr 12 '17 at 17:14
  • There might be a second cell prototype with identifier "cell" in your storyboard. Triple check. – shallowThought Apr 12 '17 at 17:18
  • I didn't see anything else with the "cell" identifier in my storyboard (or in any of my classes). If it helps, I am able to replace all the code within the tableView func above with the code found in the second to last tableView func in the following link (where it creates and returns cells like above) and it still works: https://stackoverflow.com/questions/33234180/uitableview-example-for-swift/33234181#33234181 It makes me wonder if maybe it's not properly referencing the new CustomCell class? – Su_toL Apr 12 '17 at 17:30

3 Answers3

0

Well, I'm rather silly, but posting this as an answer in case it actually helps anybody. I accidentally left the following line in the viewdidload function, which caused it to basically be ignored. Anyways, thanks to those who tried to help!

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

^ That line was from the simpler version of the UITableView tutorial that can be found through the link I posted in the question. It was left in at the end of the viewdidload function accidentally when I tried to make my table more complex.

Su_toL
  • 167
  • 2
  • 15
  • 1
    If you are using storyboard you don't need to register any cell at all. I'm wondering how many tutorials suggest that. However you **do** need to register cells when using extra .xibs. – vadian Apr 12 '17 at 18:34
0

I'm going to put some line of code, hope it will help you, if you don't understand then comment below.

let cellReuseIdentifier = "cell"

Put above line of code above your override func viewDidLoad() shown in image below

enter image description here

Now Put the below line of code in you viewDidLoad method:

yourTableviewObjc.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

Then, use below line of code in your func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell:MyCustomCellModel = yourTableviewObjc.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCellModel

It will works.

Thanks

Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
  • Thanks for the reply! I managed to fix my code by doing what I stated in my self answer, but I'm sure this will help others who land here. – Su_toL Apr 12 '17 at 20:33
  • Great :) Happy coding, help this answer to reach other by voting it up, Thanks @Gabe – Abhishek Mitra Apr 12 '17 at 20:35
0

Like you said in your question your get an error in this method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //  ***** Error triggers on the line below ******
    let cell:MyCustomCellModel = self.plantList.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCellModel

    cell.plantImage.backgroundColor = self.colors[indexPath.row]
    cell.plantLabel.text = self.animals[indexPath.row]

    return cell
}

In this method, I have noticed that plantList is an Outlet of TableView that your add in Storyboard and you tried to dequeue a cell from this table.

Why don't you dequeue MyCustomCellModel from _tableView parameter object?

I think plantList does a contain a MyCustomCellModel cell that why it is giving you an error. Instead to dequeue a cell from plantList try to dequeue a cell from _tableView.

Usman Javed
  • 2,437
  • 1
  • 17
  • 26