TableView上的didSelectRowAtIndexPath

时间:2016-01-23 16:57:55

标签: swift

不确定我做错了什么。我收到这个错误:

  

致命错误:在打开Optional时意外发现nil   此行中的值self.profileImage.image = UIImage(data: data)

在我didSelectRowAtIndexPath之后。我不想发送任何东西作为发件人,但当我尝试执行SegueWithIdentifier时,它似乎试图获取该单元格?为什么我选择它后会再次调用configureCell函数?

  

的ViewController

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        victim = friends[indexPath.row] as Friend!
        DataService.shared.setVictim(victim!)
        print("It got here with friend: \(victim!)")
        performSegueWithIdentifier("commentFromSegue", sender: nil)
}
  

FriendsTableViewCell

   class FriendTableViewCell: UITableViewCell {

    @IBOutlet weak var friendName: UILabel!
    @IBOutlet weak var profileImage: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        layer.cornerRadius = 5.0
    }

    func configureCell(friend: Friend) {
        let url = NSURL(string: friend.profileImg)
        if let data = NSData(contentsOfURL: url!) {
          self.profileImage.image = UIImage(data: data)
          // works if I comment out the above line. but does not show profile image
        }
        friendName.text = friend.friendName
        }
    }

0 个答案:

没有答案