UIImageView.image 在我的 UITableViewCell 中是 nil 当我 select 单元格时,即使我知道它应该有一个值

UIImageView.image is nil in my UITableViewCell when I select cell even though I know it should have a value

在我的 cellForRowAtIndexPath 函数中,我为 UIImageView 设置了 URL 以从中加载图像。加载此图像时,我 select 单元格,而在 didSelectRowWithIndexPath 中,单元格的图像为零。事实上,即使是标签也是零。所有数据都是零,好像单元格中没有数据,但我可以清楚地看到单元格中包含我想要的数据。

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCellWithIdentifier("universityCell", forIndexPath: indexPath) as! UniversityTableCell

    if (indexPath.row < Session.sharedInstance.universities.count) {

        let university = Session.sharedInstance.universities[indexPath.row]
        cell.setInfo(university.imageUrl)
    }

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.dequeueReusableCellWithIdentifier("universityCell", forIndexPath: indexPath) as! UniversityTableCell

    // This is nil. But why? I set the data in my cellForRowAtIndexPath method
    print(cell.universityImage.image)

}

在我的 UniversityTableCell 中:

class UniversityTableCell: UITableViewCell {

    @IBOutlet var universityImage: UIImageView!

    func setInfo(url: String) {
        self.universityImage.setImageWithURL(NSURL(string: url), usingActivityIndicatorStyle: .Gray)
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.universityImage.sd_cancelCurrentImageLoad()
    }
}

尝试将您的代码修改为

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      let cell = tableView.cellForRowAtIndexPath(indexPath) as! UniversityTableCell

     // This is nil. But why? I set the data in my cellForRowAtIndexPath method
      print(cell.universityImage.image)

}