首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CollectionView致命错误:在展开可选值时意外找到零

CollectionView致命错误:在展开可选值时意外找到零
EN

Stack Overflow用户
提问于 2015-05-15 00:34:44
回答 2查看 2.9K关注 0票数 0

我试图在视图load方法中定义的数组中添加一些图像的集合视图。这个应用程序构建并运行良好,但是当我导航到这个名为"Portfolio“的特定部分时,这个应用程序就崩溃了,给了我这个控制台。

致命错误:在展开可选值时意外找到零

有人能从下面的代码中找出发生了什么吗?我试着用if语句包装这个问题,这样它就不会崩溃,但是图像根本不会显示。如果我只是定义了一个UIBackgroundColor,它的工作方式应该是正确的。

代码语言:javascript
复制
import UIKit

class PortfolioView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var array:[String] = []

override func viewDidLoad() {

    array = ["PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7"]


    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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


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

    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell

    cell.imageView.image = UIImage(named: array[indexPath.row])


    cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2
    cell.imageView.clipsToBounds = true


    cell.imageView.layer.borderWidth = 2.0
    cell.imageView.layer.borderColor = UIColor.whiteColor().CGColor


    return cell


}

}

EN

回答 2

Stack Overflow用户

发布于 2015-05-15 00:46:56

从表面上看,由于您没有提供任何相关信息,并且只是猜测,您似乎已经定义了一个具有CollectionViewCell属性的类imageView,但是您没有通过连接到该imageView属性的出口连接到情节提要/ nib中的任何图像视图。因此,在加载时,您的imageView属性为零。

票数 0
EN

Stack Overflow用户

发布于 2016-08-04 09:22:19

在您的示例中,单元格无法找到imageView并崩溃,因为它发现它为零。在故事板上给你的imageView标签上写着"100“。然后,在单元格排出队列后,在代码中添加如下行:

let imageView = cell.viewWithTag(100) as! UIImageView imageView.image = UIImage(named: array[indexPath.item])

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30249692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档