I'm trying to build a generic UITableViewController subclass in Swift that will accommodate any number of different kinds of table view cells without having internal knowledge of any of them.
To do this I'm trying to use protocols for my models and for my table view cells. The protocol for the models will return which cell class I should be going to, and the protocol for the cells will return answers to questions like what the height of the cell should be for a given model.
But I'm having a problem making the protocols work, because with the second protocol I want to go to the cell's class rather than its instance.
The protocol for the models is as follows:
protocol JBSTableItemDelegate
{
func tableCellDelegate() -> JBSTableViewCellInterface
}
The protocol for the cells is as follows:
protocol JBSTableViewCellInterface: class
{
static func registerNibsWithTableView(tableView: UITableView)
static func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath?, tableItem: JBSTableItemDelegate) -> CGFloat
static func tableView(tableView: UITableView, dequeueReusableCellWithIndexPath indexPath: NSIndexPath, tableItem: JBSTableItemDelegate, delegate: AnyObject) -> JBSTableViewCell
}
Notice the use of the keyword "static". These methods are class methods in the UITableViewCell subclasses, and adding static seems to be what I need to do to verify that these classes conform, or so I understand.
When I use the first protocol the code looks like so, and it compiles:
let tableViewCellInterface = tableItem!.tableViewCellInterface()
It's calling this method (as one example):
func tableViewCellInterface() -> JBSTableViewCellInterface
{
return JBSLiteratureTableViewCell.self as! JBSTableViewCellInterface
}
This returns the class of the cell, such as, "JBSLiteratureTableViewCell.self"
When I use the second protocol the code looks like so, and it does not compile:
returnFloat = tableViewCellInterface.tableView(tableView, heightForRowAtIndexPath: indexPath, tableItem: tableItem!)
It fails to compile because of the static keyword up earlier, and the compiler error I get is:
'JBSTableViewCellInterface' does not have a member named 'tableView'
If I take the static keywords off of the protocol funcs, it compiles, but then the UITableViewCell subclasses complain saying:
'JBSLiteratureTableViewCell' does not conform to protocol 'JBSTableViewCellInterface'
This is because they're now trying to make sure instance methods exist, which is not where these are.
How do I make a swift class conform to a protocol at the class level, so it can be my delegate rather than some instance of a class? I'm sure I could get around this by creating helper classes of protocol JBSTableViewCellInterface that are singletons and let them do the work, but I'd rather build it right into the UITableViewCell subclasses in their class methods.