2016-11-29 8 views
2

のセルを持つHorizontal CollectionViewがあります。私がしたいことは、デフォルトの選択されたセルに設定されます。選択したセルのイメージカラーを変更する必要があります。その後、ユーザーが他のセルを選択した場合、そのセルを選択し、デフォルトのセルを選択解除する必要があります。現在、それらのすべてが動作しています。しかし、時々私がスクロールして、セルを選択すると、それは次のようにクラッシュします。ここでスクロールしてセルを選択した後にUICollectionViewがクラッシュする

ERROR: unexpectedly found nil while unwrapping an Optional value

は私の完全なコードは、コード

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

のこの部分にあなたは基本的にハードas!を使用して、それをキャストしているので、これは基本的に、あなたの質問に答える

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 
     } 
    } 
} 
+0

ハイライトのエラーメッセージ –

+0

if let構文を使用して、私たちにそれがどの方法では、クラッシュ少なくともラインを表示することができますか? – Legoless

+0

私はdidSelectItemAtIndexPathメソッドでindexPath.rowを印刷しようとしましたが、問題はindexPathがnil値を返すことです。これはエラーメッセージです。致命的なエラーです。オプション値をアンラッピングしている間に予期せずnilが見つかりました – Bishan

答えて

2
 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() 

    } 

ですcollectionView.cellForItemAtIndexPath(indexPath)の値がnilの場合はアプリがクラッシュする

はまた、あなたはまた、代わりにguard else

if let cell = collectionView.cellForItemAtIndexPath(indexPath) { 
    let aCell = cell as! CategoryCell 
    // do stuff here 
} 
+1

聖なる瞬間!私の一日を節約するためにたくさんのことがあります。感謝します。 – Bishan

+0

ようこそ。 –

関連する問題