2017-01-22 5 views
0

セルに関連付けられたファイルがデバイスにある場合、コレクションビューのセルに追加してイメージを追加しようとしています。コレクションビューに画像を追加ファイルが存在する場合はセル

ファイルがあるので、以下のコードでイメージが表示されますが、オプションでアンラップしようとするとエラーが発生します。

コードに問題がありますか?

enter image description here

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell: JourneyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! JourneyCollectionViewCell 

    // query if file is on LDS and add image to indicate 
    let cellPartName = self.partArray[indexPath.item].name 
    let checkQuery = PFQuery(className: "downloadedAudio") 
     checkQuery.whereKeyExists(cellPartName) 
     checkQuery.fromLocalDatastore() 
     checkQuery.getFirstObjectInBackground(block: { (object, error) in 
      if error != nil || object == nil { 
       print("The file does not exist locally on the device, hide the image.") 
       //cell.ImageDownloaded.image = UIImage(named: "") 

       // crashes on this line 
       cell.ImageDownloaded.isHidden = true 
      } else { 
       print("the file already exists on the device, show the image.") 
       //cell.ImageDownloaded.image = UIImage(named: "download") 

       // crashes on this line 
       cell.ImageDownloaded.isHidden = false 
      } 
     }) 


    return cell 

} 




the file already exists on the device, show the image. 
fatal error: unexpectedly found nil while unwrapping an Optional value 
(lldb) 

画像 "ダウンロード" のカセットです。

+0

「セル」はどこに定義されていますか? – BallpointBen

+0

クラッシュは何を言いますか?それについてのほとんどの詳細が役に立つでしょう。 – raidfive

+0

あなたのメソッド内で 'cell'変数が定義されていないようです。あなたはセルをデキューするのを忘れましたか? 'cell = collectionView.dequeueReusableCell(withReleaseIdentifier:reuseIdentifier、for:indexPath);' – ilbesculpi

答えて

2

ワンクイックノート。変数を宣言するときは、常にcamelCaseを使用する必要があります。したがって、ImageDownloadedimageDownloadedである必要があります。ここでのコードの非常に数行で

、このラインにクラッシュするようだ:

cell.ImageDownloaded.isHidden = false 

これは、変数ImageDownloadedはおそらくnilある変数であることを意味しています。私の経験上、これはあなたのセルクラスのコードで変数が宣言されているが、関連するUIImageViewは宣言に接続されていないという事実のためかもしれません。したがってストーリーボードからは、それが存在するように見え、コードからは存在するように見えますが、アクセスしようとすると突然破損します。

これは、いずれかの部分を削除して貼り付けた場合に発生する可能性があります。同じように見えますが、接続はもう存在しません。これを修正するには、コードの宣言の横にある空の円の中に再度ドラッグ&ドラッグします。

+0

あなたは正しいですか?私はミューペットですし、インターフェイスビルダーから私のクラスへのリンクが壊れていました。 – Pippo

関連する問題