Ios UICollectionView在滚动并选择单元格后崩溃

Ios UICollectionView在滚动并选择单元格后崩溃,ios,swift,xcode,Ios,Swift,Xcode,我有一个水平的CollectionView,里面有一个图像视图的单元格。我要做的是设置一个默认的选定单元格。所选单元格应更改其图像颜色。若用户选择其他单元格,则应选择该单元格,并取消选择默认选定单元格。目前所有这些都在发挥作用。但有时当我滚动并选择一个单元格时,它会崩溃,如下所示 错误:在展开可选值时意外发现nil 这是我的完整代码 override func viewDidLoad() { super.viewDidLoad() self

我有一个水平的CollectionView,里面有一个图像视图的单元格。我要做的是设置一个默认的选定单元格。所选单元格应更改其图像颜色。若用户选择其他单元格,则应选择该单元格,并取消选择默认选定单元格。目前所有这些都在发挥作用。但有时当我滚动并选择一个单元格时,它会崩溃,如下所示

错误:在展开可选值时意外发现nil

这是我的完整代码

    override func viewDidLoad() {
            super.viewDidLoad()
            self.collectionView.delegate = self
            self.locationManager.delegate = self
            collectionView.allowsMultipleSelection = false
        }

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


        func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

                let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CategoryCell
                cell.CarImage.image = UIImage(named: CatImages[indexPath.row])?.maskWithColor(UIColorFromRGB(0xEA7C6A))
                cell.Name.textColor = UIColorFromRGB(0xEA7C6A)

        }

        func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {

                let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CategoryCell
                cell.CarImage.image = UIImage(named: CatImages[indexPath.row])
                cell.Name.textColor = UIColor.darkGrayColor()

        }

        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CategoryCell

            cell.Name.text = CatNames[indexPath.row]


            // setup selectedBackgroundView
            if(cell.selected == true){
                cell.CarImage.image = UIImage(named: CatImages[indexPath.row])!.maskWithColor(UIColorFromRGB(0xEA7C6A))
                cell.Name.textColor = UIColorFromRGB(0xEA7C6A)
            }
            else{
                cell.CarImage.image = UIImage(named: CatImages[indexPath.row])
                cell.Name.textColor = UIColor.darkGrayColor()
            }


            return cell
        }


        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            let indexPath = NSIndexPath(forRow: 0, inSection: 0)
            self.collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: .None)
        }

extension UIImage {
    func maskWithColor(color: UIColor) -> UIImage? {

        let maskImage = self.CGImage
        let width = self.size.width
        let height = self.size.height
        let bounds = CGRectMake(0, 0, width, height)

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
        let bitmapContext = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue) //needs rawValue of bitmapInfo

        CGContextClipToMask(bitmapContext, bounds, maskImage)
        CGContextSetFillColorWithColor(bitmapContext, color.CGColor)
        CGContextFillRect(bitmapContext, bounds)

        //is it nil?
        if let cImage = CGBitmapContextCreateImage(bitmapContext) {
            let coloredImage = UIImage(CGImage: cImage)

            return coloredImage

        } else {
            return nil
        }
    }
}
这基本上回答了您的问题,因为在代码的这一部分

   let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CategoryCell
你基本上是在用as硬铸它!如果collectionView.cellForItemAtIndexPathindexPath的值为零,则应用程序将崩溃

或者,您也可以使用if-let语法而不是guard-else


突出显示错误消息,并至少向我们显示它崩溃的行,在哪种方法中?我尝试在didSelectItemAtIndexPath方法中打印indexPath.row,问题是indexPath返回nil值。这是错误消息im get-致命错误:在展开可选值时意外发现nil!!谢谢你救了我一天。谢谢。
   let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CategoryCell
if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
    let aCell = cell as! CategoryCell
    // do stuff here
}