【问题标题】:SWIFT 2 - Impossibile to pass value from UICollectionViewController to UICollectionViewCell. Found NILSWIFT 2 - 不可能将值从 UICollectionViewController 传递到 UICollectionViewCell。发现无
【发布时间】:2015-11-01 14:35:00
【问题描述】:

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

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

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")
    }

}

【问题讨论】:

    标签: ios swift uicollectionviewcell


    【解决方案1】:

    你需要删除线

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

    因为它用简单的类注册替换了情节提要中的单元注册,其中包含所有出口定义。这意味着当您调用dequeueReusableCellWithReuseIdentifier 时,单元格不会从情节提要中取消存档,并且不会存在任何出口。

    【讨论】:

    • 如果我删除该行,我会收到错误:致命错误:init(coder:) 尚未实现:
    • 那是因为你已经实现了那个方法来抛出那个异常。你不能那样做……
    • init(coder:) 是从情节提要创建视图时调用的 init 方法。您自己编写代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2020-09-27
    相关资源
    最近更新 更多