3

I'm trying to create static cells in a UITableViewCell. I've started by adding 2 cells in Interface Builder and given them the identifier title and news. Then I've created an array with these two identifiers and added it to cellForRowAtIndexPath. However I keep getting following error:

'unable to dequeue a cell with identifier title - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

The array

var menuItems = ["title", "news"]

tableView delegate methods

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 2
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 64
}

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

    let cell = tableView.dequeueReusableCellWithIdentifier(menuItems[indexPath.row], forIndexPath: indexPath) as! UITableViewCell

    cell.backgroundColor = UIColor(white: 0.2, alpha: 1.0)

    var bottomView: UIView = UIView(frame: CGRectMake(0, cell.frame.height-1, cell.frame.width, 1))
    bottomView.backgroundColor = UIColor(white: 0.2, alpha: 0.15)
    cell.contentView.addSubview(bottomView)

    return cell
}
5
  • Why are you creating two cell at interfacebuilder? Just create one as "title" or "news" Commented Apr 25, 2015 at 12:44
  • have you set the cell identifiers in interface builder? Commented Apr 25, 2015 at 12:45
  • yes i've set one to title and one to news Commented Apr 25, 2015 at 12:45
  • did you connect your subclass of UITableViewController with the tableViewController in interface builder? Commented Apr 25, 2015 at 12:47
  • yes i i've set the class to my tableViewController (SideTableViewController) Commented Apr 25, 2015 at 12:49

2 Answers 2

3

It seems like you have to register the UITableViewCell class manually for the cell. You can achieve this by calling

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: menuItems[indexPath.row])

in either your viewDidLoad() function of in the cellForRowAtIndexPath function of UITableViewDataSource

Sign up to request clarification or add additional context in comments.

Comments

1

Like the error message describes, you must register them if you created them as an empty UI file. This can simply be placed in the viewDidLoad function.

override func viewDidLoad() {
        super.viewDidLoad()

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

        var nib2 = UINib(nibName: "AppleCell", bundle: nil)
        tableView.registerNib(nib2, forCellReuseIdentifier: "apples")
}

2 Comments

i dont have a nib file? why is that needed
@PeterPik it is only needed when you are using custom cells. If you are using standard UITableViewCells, try tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "YOUR_CELL_IDENTIFIER"). Do this for both of your cell identifiers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.