加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

标签出口在自定义UICollectionViewCell在Swift导致可选.没有崩溃

发布时间:2020-12-14 05:21:45 所属栏目:百科 来源:网络整理
导读:我有一个collectionViewController,我想显示一些自定义UICollectionViewCell与他们上的一些标签.不幸的是,每当我尝试访问自定义UICollectionViewCell的标签,它会导致崩溃: 安慰 fatal error: Can’t unwrap Optional.None 窗口 Thread1: EXC_BAD_INSTRUCTIO
我有一个collectionViewController,我想显示一些自定义UICollectionViewCell与他们上的一些标签.不幸的是,每当我尝试访问自定义UICollectionViewCell的标签,它会导致崩溃:

安慰

fatal error: Can’t unwrap Optional.None

窗口

Thread1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)

我正在尝试访问标签:

cell.name.text = names[indexPath!.item]

也许这是从我的出口标签是零?但是看看周围的答案没有任何工作,因为我不确定什么问题添加?/!在我的代码中并没有真正的帮助.

MyCustomUICollectionViewController

class ScrambledTextCollectionViewController: UICollectionViewController {

    var names: String[] = ["Anna","Alex","Brian","Jack"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView.registerClass(MyCustomCollectionViewCell.self,forCellWithReuseIdentifier: reuseIdentifier)

    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView?,numberOfItemsInSection section: Int) -> Int {
        return names.count
    }

    override func collectionView(collectionView: UICollectionView?,cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
        var cell = collectionView?.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as MyCustomCollectionViewCell

        cell.name.text = names[indexPath!.item]

        return cell
    }
}

MyCustomCollectionViewCell

class MyCustomCollectionViewCell: UICollectionViewCell {

    @IBOutlet var name: UILabel
    init(frame: CGRect) {
        super.init(frame: frame)                
    }
}
找到答案 here

Remove,self.collectionView.registerClass(MyCustomCollectionViewCell.self,forCellWithReuseIdentifier:reuseIdentifier)

阅读链接的详细原因为什么

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读