のセルを持つ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
}
}
}
ハイライトのエラーメッセージ –
の
if let
構文を使用して、私たちにそれがどの方法では、クラッシュ少なくともラインを表示することができますか? – Legoless私はdidSelectItemAtIndexPathメソッドでindexPath.rowを印刷しようとしましたが、問題はindexPathがnil値を返すことです。これはエラーメッセージです。致命的なエラーです。オプション値をアンラッピングしている間に予期せずnilが見つかりました – Bishan