Apocal Apocal - 3 months ago 18
Swift Question

Custom Cell Segue Sending Data to Second View Controller Not Working

I have a custom cell that I am using dequeueReusableCellWithIdentifier to display. It is populated by a plist file.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("challengeCell", forIndexPath: indexPath) as! ChallengeCell
// set labels
cell.ChallengeLabel.text = challengeLevels[indexPath.row].uppercaseString
cell.ChallengeMaxPoints.text = "Score: " + challengeMaxScore[indexPath.row]
cell.SizeLabel.text = "Size: " + challengeSize[indexPath.row]
// set color
cell.ChallengeLabel.textColor = UIColor.whiteColor()
cell.ChallengeMaxPoints.textColor = UIColor.whiteColor()
cell.SizeLabel.textColor = UIColor.whiteColor()
return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return challengeLevels.count
}


I am working on getting this to segue properly based on all of the guides that I've found here and elsewhere:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
challengeSelected = indexPath.row
print("Send selected \(challengeSelected)")

self.performSegueWithIdentifier("ChallengeSelected", sender: challengeSelected);
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "ChallengeSelected") {
if let nextViewController = (segue.destinationViewController as? LevelListViewController)
{
print(challengeSelected)
nextViewController.selectedChallenge = challengeSelected
}
}
}


The associated variable is assigned above in this class ChallengeListViewController as per the below:

var challengeSelected: Int = 0


The code doesn't error out, but it also doesn't send the value. The corresponding ViewController list as per the below:

class LevelListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var selectedChallenge: Int = 0
let getLevels = LevelList(challengeNumber: 1)
var puzzleLevels: [String] = []
var solvedStatus: [String] = []

// UITableViewDataSource for Table
override func viewDidLoad() {
super.viewDidLoad()

print ("Receive Selected: \(selectedChallenge)")
//Initalize Arrays
var x = 0
for _ in getLevels.levels {
puzzleLevels.append(getLevels.levels[x][0] as! String)
x = x + 1
}

//print (puzzleLevels.count)
}

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

override func prefersStatusBarHidden() -> Bool {
return true
}

//How to create a table
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("levelListCell", forIndexPath: indexPath) as! LevelListCell

// set labels
cell.levelNameLabel.text = puzzleLevels[indexPath.row].uppercaseString
cell.levelStatusLabel.text = "Not Solved"

// set color
cell.levelNameLabel.textColor = UIColor.whiteColor()
cell.levelStatusLabel.textColor = UIColor.whiteColor()
return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return puzzleLevels.count
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("selected \(indexPath.row)")
}

}


Edit:

Screen shots of segues follows below:

Segue Screen Shot #1
Segue Screen Shot #2

Help is really appreciated. I am stumped.

Thank you,

Rich

Answer

When performing a segue within your code, take care not to also have a segue set up in your storyboard from the cell itself. The segue needs to be created from the view controller. Otherwise, when the cell is selected, the segue is occurring without giving your code the proper chance to run its course.

Delete the current segue in storyboard. Then CTRL-drag from the viewController (not the cell) to LevelListViewController and name it "ChallengeSelected". Now, when you select a cell, the didSelectRowAtIndexPath() will fire first and will call performSegueWithIdentifier()

enter image description here

Comments