I have a tableView (not static cells) with two sections A bar section and a club section( and each has several cells). I want that every cell from the same section goes to the same viewcontroller.
I can only acces the first one, never the last one. Even de cells from the second section go to the first viewcontroller.
Can someone see my mistake?
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if section == 0 {
return areas.bars.count
} else {
return areas.clubs.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mainIdentifier", forIndexPath: indexPath)
if indexPath.section == 0 {
let bars = areas.bars
let bar = bars[indexPath.row]
cell.textLabel?.text = bar.name
return cell
} else {
let clubs = areas.clubs
let club = clubs[indexPath.row]
cell.textLabel?.text = club.name
return cell
}
}
the segue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "barsandclubsIdentifier"{
let selectedIndex = self.tableView.indexPathForSelectedRow!
let selectedBar = self.areas.bars[selectedIndex.row]
let detailBarsAndClubsViewController = segue.destinationViewController as! DetailBarOrClubViewController
detailBarsAndClubsViewController.bars = selectedBar
}
else {
let selectedIndex = self.tableView.indexPathForSelectedRow!
let selectedClub = self.areas.clubs[selectedIndex.row]
let detailBarsAndClubsTwoViewController = segue.destinationViewController as! DetailBarOrClubTwoViewController
detailBarsAndClubsTwoViewController.clubs = selectedClub
}
Each prototype cell can only segue to a single viewController. If you have 2 different destination viewControllers, you need 2 prototype cells: 1 for bars and 1 for clubs. Give them each a unique identifier such as "barCell"
and "clubCell"
.
Then in cellForRowAtIndexPath
, dequeue the proper cell:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellID = ["barCell", "clubCell"][indexPath.section]
let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
Then you can wire each prototype cell to go to the appropriate viewController. Give each of these two segues a unique identifier such as "barSegue"
and "clubSegue"
and then you can use those in prepareForSegue
to configure the destination viewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "barSegue"{
let selectedIndex = self.tableView.indexPathForSelectedRow!
let selectedBar = self.areas.bars[selectedIndex.row]
let detailBarViewController = segue.destinationViewController as! DetailBarViewController
detailBarsViewController.bars = selectedBar
}
else if segue.identifier = "clubSegue" {
let selectedIndex = self.tableView.indexPathForSelectedRow!
let selectedClub = self.areas.clubs[selectedIndex.row]
let detailClubViewController = segue.destinationViewController as! DetailClubViewController
detailClubsViewController.clubs = selectedClub
}
}