自定义单元格Segue将数据发送到第二个视图控制器不起作用

时间:2016-08-29 23:53:41

标签: swift segue

我有一个自定义单元格,我正在使用dequeueReusableCellWithIdentifier来显示。它由plist文件填充。

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
}

我正在根据我在这里和其他地方找到的所有指南正确地实现这一目标:

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
        }
    }
}

关联变量在此类ChallengeListViewController中按照以下内容分配:

var challengeSelected: Int = 0

代码没有错误输出,但它也没有发送该值。相应的ViewController列表如下:

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)")
    }

}

编辑:

segue的屏幕截图如下:

Segue Screen Shot #1 Segue Screen Shot #2

帮助非常赞赏。我很难过。

谢谢,

1 个答案:

答案 0 :(得分:2)

在代码中执行segue时,请注意不要在单元格本身的故事板中设置segue。需要从视图控制器创建segue。否则,当选择单元格时,会发生segue,而不会给你的代码提供正确的运行路径。

删除故事板中的当前segue。然后按住CTRL键将从viewController (不是单元格)拖到didSelectRowAtIndexPath(),并将其命名为#34; ChallengeSelected"。现在,当您选择一个单元格时,performSegueWithIdentifier()将首先触发,然后调用didSelectRowAtIndexPath()

MongoDB Node tutorial: ssl connection

修改

如果您要在didSelectRowAtIndexPath()中查找的所有内容都获得执行segue的行,则可以保留原始设置,让单元格从故事板中删除,删除prepareForSegue()并在challengeSelected = self.tableView.indexPathForSelectedRow?.row nextViewController.selectedChallenge = challengeSelected 做:

{{1}}