0

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?

3
  • What cell reuse identifier have you set in the storyboard? Commented Sep 19, 2015 at 20:02
  • i set the identifier as Cell in the storyboard Commented Sep 19, 2015 at 20:03
  • The tableViews were working before I tried to access the array from the tabBarViewController and then stopped once I implemented that code. I feel like that error could be misleading. Commented Sep 19, 2015 at 20:08

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.