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

时间:2016-11-29 13:06:33

标签: ios swift xcode

我有一个Horizontal CollectionView,里面有一个image view的单元格。我想要做的是设置默认选定的单元格。选定的单元格应更改其图像颜色。如果用户选择了其他一些单元格,则应该选择它,并且应该取消选择默认选择的单元格。目前所有这些都在起作用。但有时当我滚动并选择一个单元格时,其崩溃的说法如下。

  

错误:在展开“可选”值时意外发现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
        }
    }
}

1 个答案:

答案 0 :(得分:3)

     func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
            guard collectionView.cellForItemAtIndexPath(indexPath) != nil else {
                  return
            }
            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) {
            guard collectionView.cellForItemAtIndexPath(indexPath) != nil else {
                  return
            }
            let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CategoryCell
            cell.CarImage.image = UIImage(named: CatImages[indexPath.row])
            cell.Name.textColor = UIColor.darkGrayColor()

    }

这基本上回答了你的问题,因为代码的这一部分

   let cell = collectionView.cellForItemAtIndexPath(indexPath) as! CategoryCell

您使用as!基本上难以投射它,如果collectionView.cellForItemAtIndexPath(indexPath)的值为零,则应用会崩溃

或者,您也可以使用if let语法代替guard else

if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
    let aCell = cell as! CategoryCell
    // do stuff here
}
相关问题