简体   繁体   中英

How to programmatically add an UILabel to my UICollectionViewCell

I have a custom UICollectionViewCell and it has a scrollView as its child. I want to programmatically add an UILabel to the scrollView.

class MyUICollectionViewCell : UICollectionViewCell {
    var newLabel = UILabel?
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        var labelFrame = CGRect(x: 10, y: 439, width: 355, height:0);
        newLabel = UILabel(frame: labelFrame)

        self.scrollView.addSubview(newLabel!)
    }
}

I tried doing it in the init() or init(code aDecoder :NSCoder) method. But I get an exception saying my scrollView is nil. Can you please tell me where is the best place to add my UILabel?

Add the label in awakeForNib. IBOutlets are still nil at the time an init method is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM