Ryguyu
Ryguyu

Reputation: 173

Error: "unable to dequeue a cell with identifier Cell"

I am using the following viewController setup

enter image description here

What I am trying to do is access an array of places from the placesTabBar and show them in the two navigationControllers. One of these controllers will contain a list of places while the other one will be used for only places you are attending. On selection of a cell it will update the other view controller when switched over to say that you are going to that place. I am using the following code:

AllPlaces ViewController

class AllPlaces: UITableViewController {

var delegate:placesTabBar!

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


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel?.text = delegate.placeDataArray[indexPath.row].description

    return cell
}

}

AttendingPlaces ViewController class AttendingPlaces: UITableViewController {

var delegate:placesTabBar!

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return delegate.placeDataArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel?.text = delegate.placeDataArray[indexPath.row].description
    return cell
}

}

TabBarController

class placeData: Equatable {
var description : String
var selected : Bool

init (description : String, selected : Bool) {
    self.description = description
    self.selected = selected
}
}
class placesTabBar: UITabBarController, CustomTabBarDelegate {

var placeDataArray = Array<placeData>()

override func viewDidLoad() {

    placeDataArray = [placeData(description: "Afghanistan", selected: false), placeData(description: "Albania", selected: false)]

    var table1 = AttendingPlaces()
    var table2 = AllPlaces()

    table1.delegate = self
    table2.delegate = self

    var navController1 = UINavigationController(rootViewController: table1)
    var navController2 = UINavigationController(rootViewController: table2)

    self.viewControllers = [navController1, navController2]

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Right now I am just trying to load the tables with the two places I have in the array. However, I am getting a "unable to dequeue a cell with identifier Cell" error. The reuse identifier is set properly in both tableViewControllers so i'm not sure what is going on. Could it be the way I am trying to share the data between the two viewControllers?

Upvotes: 0

Views: 217

Answers (1)

pbasdf
pbasdf

Reputation: 21536

You're getting that error because you are creating new instances of the two table view controllers (eg. table1 = AttendingPlaces()) which are not associated with the storyboard, and therefore do not "know about" the identifiers set up in the storyboard.

You should be able just to get references to the existing table view controllers which are instantiated at the same time as the tabBarController:

var nav1 = self.viewControllers[0] as! UINavigationController
var table1 = nav1.topViewController as! AttendingPlaces
var nav2 = self.viewControllers[1] as! UINavigationController
var table2 = nav2.topViewController as! AllPlaces

table1.delegate = self
table2.delegate = self

Upvotes: 1

Related Questions