nicolasgomollon/LPRTableView

Using LPRTableViewController as Class Inheritance Results in "unable to dequeue a cell with identifier"

Closed this issue · 5 comments

s1rc commented

Following your example I set my ViewController for my TableViewController View to inherit LPRTableViewController and I am getting "unable to dequeue cell" errors.

The cells exist, they are Custom Prototypes in the TableViewController, no custom classes. If I modify

func registerClasses() {
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "newIdentifier")
}

Then I have to unwrap all the labels and no values are displayed.

If I leave my TableViewController inheritance as UITableViewController and set the table view of the TableViewController to LPRTableView it does work, but hideDraggingView is never triggered.

You only need to implement the registerClasses() method if you are using a custom class, or your Cell Reuse Identifier is different from the default value of "Cell".

If the identifier in your Custom Prototype Cell is any different, then you do need to implement the registerClasses() method, and you also need to make sure that this identifier matches the Cell Reuse Identifier when dequeuing the cell:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("newIdentifier", forIndexPath: indexPath) as MyCustomTableViewCell
    // Set cell data here.
    return cell
}

Let me know if this fixes it, so I can close this issue.

Works great but I can not figure out why the cell background color won't change. cell.backgroundColor is commented out in draggingCell function and in my storyboard it's set to a light blue but shows up white.

s1rc commented

I have implemented the registerClasses() method with my custom Cell Reuse Identifier, however none of the custom UILabels, or UIImageViews can be used by their Tags giving an optional unwrapping error. When unwrapped the values are not set.

I'm fairly certain using UITableViewCell.self as the class overrides any customization I've done with the cell prototypes in the StoryBoard.

What class should I be using when registering the prototype?

@mmoore410 Make sure that you’re setting the cell’s background color within the tableView(tableView:cellForRowAtIndexPath:) data source method:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    // Change cell background color here:
    cell.backgroundColor = UIColor(red: 165.0/255.0, green: 228.0/255.0, blue: 255.0/255.0, alpha: 1.0)
    return cell
}

@mattwitkowicz I tend to stay away from using Interface Builder, so I’m not too familiar with how it works, but in code you would create a custom UITableViewCell subclass with your own labels, etc. This also increases code reusability, and leaves your table view code cleaner.

As a side note: it’s considered bad style to access UI elements by their tags. I’d recommend looking into making a custom cell subclass, whether that’s in code or in IB, doesn’t matter.

I’ll close this issue for now, since it can be solved by creating your own custom UITableViewCell subclass.