Silver Challenge help needed


#1

I have no clue what I’m supposed to do, can anyone give me some hints and/or a solution?

Thanks in advance!
Arthur.


#2

Here’s what I did. Remove the registerClass method call in viewDidLoad():

override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") tableView.dataSource = todoList }
… then in TodoList…

[code] func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCellWithIdentifier(“Cell”, forIndexPath: indexPath)
let cell = tableView.dequeueReusableCellWithIdentifier(“Cell”) ??
UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: “Cell”)
let item = items[indexPath.row]
cell.textLabel!.text = item

    return cell
}[/code]

#3

When you Option Click the registerClass method, the declaration is like this:
func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String)

The AnyClass parameter is expecting a class type, not an objects of the class, but a class itself (that is why we call it, e.g. with UITableViewCell.self)… it does so, so it can know what kind of cells are going to be in the table view, it can be a custom subclass of UITableViewCell, for example.

So it is responsibility of TodoList to give that information.

I did it like so:


extension TodoList: UITableViewDataSource {
    
    var getCellInfo: (cellClass: AnyClass, reuseIdentifier: String) {
        return (UITableViewCell.self, "Cell")
    }

...
}

In ViewController we have:


    let todoList = TodoList()

So I ask that object for the information.


override func viewDidLoad() {
        super.viewDidLoad()
        
        let cellInfo = todoList.getCellInfo
        
        tableView.registerClass(cellInfo.cellClass, forCellReuseIdentifier: cellInfo.reuseIdentifier)
        tableView.dataSource = todoList
}

And back in TodoList.swift, I dequeue the cell, like this…


let cell = tableView.dequeueReusableCellWithIdentifier(getCellInfo.reuseIdentifier, forIndexPath: indexPath)

So there is just one “Cell” string literal.
And the ViewController doesn’t know what kind of cell is registering, it is asking the object of TodoList, and so with the reuse identifier.


#4

Hi gotfried, I think that the solution is much more simple and we only have to operate inside the storyboard:
I added a “Table View Cell” in the storyboard and inside the Attributes inspector I set the Identifier to Cell. And of course I removed the statement “tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: “Cell”)” from the ViewController.swift file.


#5

Yes, you are right, you could do it with the storyboard, and It is a pretty standard way.
But, I think the challenge was more about communication. I mean, TodoList being able to decide what kind of cells it wants (having the info within), and having a way to communicate that to the ViewController.
But yes, your solution is simpler.