如何以编程方式将UILabel添加到我的UICollectionViewCell

时间:2014-09-20 19:07:12

标签: ios swift

我有一个自定义的UICollectionViewCell,它有一个scrollView作为它的子。我想以编程方式添加 一个UILabel到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!)
    }
}

我尝试在init()或init(代码aDecoder:NSCoder)方法中执行此操作。但我得到一个例外,说我的scollView是零。你能告诉我添加UILabel的最佳位置在哪里吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

在awakeForNib中添加标签。在调用init方法时,IBOutlets仍为零。

相关问题