Registering a Nib

1.4k views Asked by At

I would like to set the values of detail labels in the View Controller file using an array with index path.row .

In a previous post: [link][1]

someone helpfully suggested I use

var nib = UINib(nibName: "YourCellSubclass", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")

however I'm new to using this and came across an error:

Cannot invoke registerNib with an argument list of type 'UINib, forCellReuseIdentifier: 'String'
1

There are 1 answers

5
Rui Peres On

My best guess would be, instead of this:

var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

Should be:

var cell: YourSubClass = self.tableView.dequeueReusableCellWithIdentifier("cell") as YourSubClass

This compiles in Xcode 7:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyCellSubclass

    return cell
}

If you have multiple kinds of cells, you can go with:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let aCell = table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    if let myCell = aCell as? MyCellSubclass {

        return myCell
    }

    return aCell
}