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

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

概述我有一个collectionViewController,我想显示一些自定义UICollectionViewCell与他们上的一些标签.不幸的是,每当我尝试访问自定义UICollectionViewCell的标签,它会导致崩溃: 安慰 fatal error: Can’t unwrap Optional.None 窗口 Thread1: EXC_BAD_INSTRUCTION(code=EXC_1 我有一个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)

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

总结

以上是内存溢出为你收集整理的标签出口在自定义UICollectionViewCell在Swift导致可选.没有崩溃全部内容,希望文章能够帮你解决标签出口在自定义UICollectionViewCell在Swift导致可选.没有崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1028503.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存