user1688346
user1688346

Reputation: 1970

unexpected error in UICollectionViewCell init

I am using Storyboard to create the controls and received this error on self.lblprice lboutlet when initialization. fatal error: unexpectedly found nil while unwrapping an Optional value

    required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.initialize()

}
func initialize(){
    self.lblPrice.font = UIFont(name: "Avenir", size: 11)
    self.imageView.contentMode = UIViewContentMode.ScaleAspectFit
    self.imageView.clipsToBounds = true
    self.lblBrand.font = UIFont(name:"Avenir-Book", size:15)
    self.lblTitle.font = UIFont(name:"Avenir", size:12)
    self.activityIndicatorView = DGActivityIndicatorView(type: .BallPulse, tintColor: UIColor.themeColor(), size: 20.0)
    self.activityIndicatorView.frame = CGRectMake(0.0, 0.0, 50.0, 50.0)
    self.imageView.addSubview(activityIndicatorView)
    self.activityIndicatorView.center = self.imageView.center
}
var activityIndicatorView: DGActivityIndicatorView!
@IBOutlet var lblMSRP: UILabel!
@IBOutlet var lblPrice: UILabel!
@IBOutlet var lblTitle: UILabel!
@IBOutlet var lblBrand: UILabel!
@IBOutlet var imageView: UIImageView!

Upvotes: 0

Views: 68

Answers (1)

the_critic
the_critic

Reputation: 12820

At the point of initialization the nib hasn't been loaded yet. Put the code into awakeFromNib() instead.

Edit: Also make sure to call super.awakeFromNib() in your awakeFromNib() as a best practice.

Sorry for short answer, I'm answering this from my phone while sitting on the loo. (I hope that doesn't affect the credibility of my answer)

Upvotes: 1

Related Questions