在 didSelectRow 方法上仍然显示 nil 变量

Still showing nil variable on didSelectRow method

courseData 是一个结构对象数组。每个对象都有 9 个字符串变量(holeNumber、Par、SI、计划等)单元格填充正常但是当您单击一行时我试图获取一个详细视图控制器以使用在可变计划下保存的图像填充图像视图数组中的相关结构。显示的完整代码。 courseImage1 变量仍然显示为零。请帮助我已经卡了3天了!

注意:var CourseImage1: UIImage?在我的代码顶部公开声明。

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

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

    let item = courseData[indexPath.row]

    let holeLabel = cell.viewWithTag(1) as UILabel
    holeLabel.text = item.holeNumber

    let parLabel = cell.viewWithTag(2) as UILabel
    parLabel.text = item.par

    let siLabel = cell.viewWithTag(3) as UILabel
    siLabel.text = item.SI

    let wyLabel = cell.viewWithTag(4) as UILabel
    wyLabel.text = item.whiteYards

    let yyLabel = cell.viewWithTag(5) as UILabel
    yyLabel.text = item.yellowYards

    let ryLabel = cell.viewWithTag(6) as UILabel
    ryLabel.text = item.redYards

    let photo = item.image
    let placeholder = cell.viewWithTag(7) as UIImageView
    placeholder.image = UIImage(named: photo)


    return cell
}

func tableView(tableView: UITableView, didselectRowAtIndexPath indexPath: NSIndexPath) {

    let courseSection = courseData[indexPath.row]

    var tipPic = courseSection.plan
    var courseImage : UIImage = UIImage(named: tipPic)!
    courseImage1 = courseImage

}

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var destViewController : DetailViewController = segue.destinationViewController as DetailViewController
        if (segue.identifier == "courseTip") {

        destViewController.receivedPic = courseImage1

    }
    }
}

 func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    (indexPath, animated: false)
}

因为didSelectRowAtIndexPath是在prepareForSegue之后触发的,所以相关逻辑都放在后一个方法中:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var destViewController : DetailViewController = segue.destinationViewController as DetailViewController
    if (segue.identifier == "courseTip") {
        // I assume "self.tableView" points to your TV, if not amend the name below
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let courseSection = courseData[indexPath.row]
            var tipPic = courseSection.plan
            var courseImage : UIImage = UIImage(named: tipPic)!
            destViewController.receivedPic = courseImage
        } else {
            println("Oops, no row has been selected")
        }
    }
}