I was stuck in a simple problem and could not find any solution.
MyCell.xib has a class fileowner MyCell : UITableViewCell .
I use it like this:
viewDidLoad method:
let nib = UINib(nibName: "MyCell", bundle: nil) self.tableView.register(nib, forCellReuseIdentifier: "myIdentifier")
tableView cellForRowAt method:
let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath) as? MyCell
It works well.
I will subclass my class to add some new methods:
class SuperCell : MyCell { func coolMethod { print("cool") } }
And try using it like this:
let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath) as? SuperCell
he returns nil
How can I make it work?
I tried to create a prototype cell in InterfaceBuilder with the identifier myIdentifier and with the SuperCell class, but that did not help.
Why do I need it
I just want to use the same view (xib) for different classes of cells.
In my case, I have a common cell ( MyCell ) with a view (xib). MyCell fully describes fields (IBOutlets). I also want to create several other cell classes that are subclasses of MyCell , but they will provide some behavior for these IBOutlets. For example, FirstMyCell : MyCell will have a setFieldsFrom(objectOne: ObjectOne) , and SecondMyCell : MyCell will have a different setFieldsFrom(anotherObject: ObjectAnother) .
Of course, I can just add these two methods to my MyCell class, but it will be unclean.