SWIFT 2 - 无法将值从 UICollectionViewController 传递到 UICollectionViewCell。发现无

SWIFT 2 - Impossibile to pass value from UICollectionViewController to UICollectionViewCell. Found NIL

我正在尝试将值从 UiCollectionViewController 传递到 UICollectionViewCell。 我在故事板中创建了一个连接到 CellView 的自定义 UICollectionViewCell。然后我创建了一个 UILabel IBOutlet。

当我尝试向我的 UICollectionViewCell 发送值时 class 我总是收到错误消息:

fatal error: unexpectedly found nil while unwrapping an Optional value

来自以下行:

videoCell.nameUsrVideo.text = "TEST"

我也看不到单元格中的任何对象

知道为什么吗?

UiCollectionViewController:

override func viewDidLoad() {

    super.viewDidLoad()

            collectionView!.registerClass(videoCellClass.self, forCellWithReuseIdentifier: "VideoCell")

    }

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("VideoCell", forIndexPath: indexPath) as! videoCellClass

        videoCell.nameUsrVideo.text = "TEST"

        return videoCell
    }

UICollectionViewCell:

class videoCellClass: UICollectionViewCell {

    @IBOutlet weak var nameUsrVideo: UILabel!

    override init(frame: CGRect) {



        super.init(frame: frame)



    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

您需要删除该行

collectionView!.registerClass(videoCellClass.self, forCellWithReuseIdentifier: "VideoCell")

因为它用简单的 class 注册替换了情节提要中的单元格注册,故事板具有所有出口定义。这意味着当您调用 dequeueReusableCellWithReuseIdentifier 并且 none 的出口将存在时,单元格不会从情节提要中取消存档。