2016-08-21 13 views
0
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? PictureCellForHomeVC { 
     picArray[indexPath.row].getDataInBackgroundWithBlock({ (data: NSData?, error: NSError?) in 
      if error == nil { 
       if let data = data { 
        let image = UIImage(data: data) 
        cell.imageView.image = image 
        return cell 
       } 
      } 
     }) 
    }else { 
     return UICollectionViewCell() 
    } 
} 

に予期しない非ボイド戻り値Iは、エラーが表示されます。上記のコードでCollectionView cellForItemAtIndexPath DataSourceメソッド

Unexpected Non Void return value in void function

私の上記のコードでpicArrayは、私が呼び出すことができますPFFileオブジェクトとのコードですgetDataInBackgroundWithBlockメソッド。

getDataInBackgroundWithBlockのcompletionHandlerがvoidを返すので、私はエラーが何を意味するのか理解しています。私はこのセルを返すことはできませんが、この問題を解決する方法に固執しています。

私はメソッドを呼び出さないため、データソースメソッドなので、メソッドcellForItemAtIndexPathに別のクロージャを渡すことさえできません。誰も私はこの問題を解決する方法を知っていますか?

+2

をブロックコードの後に​​復帰 'cell'を行います(これは非同期でなければなりません)、 '})'の後ろ、 '} else'の前です。イメージが見つからなくても、セルを返さなければなりません。 – Larme

+0

@Larmeしたがって、 'else 'の前に' return cell'を置くと、 'cellForItemAtIndexPath'が' getDataInBackgroundWithBlock'へのコールバックを返してもイメージが正しく更新されます。 – CapturedTree

答えて

1

あなたは、あなたの関数内で返しているが、実際に(それゆえエラーが許可されていない)voidを返すことになっているgetDataInBackgroundWithBlockから何かを返すようにしようとしていると思う何かを返すようにしようとしています。

あなたは(追加された効果のために)、その間にそれをプレースホルダイメージを与えながら、バックグラウンドで画像をロードし、このような何か行うことができます:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? PictureCellForHomeVC { 
     picArray[indexPath.row].getDataInBackgroundWithBlock({ (data: NSData?, error: NSError?) in 
      if error == nil { 
       self.loadImage(inImageView: cell.imageView, withData: data) 
      } 
     }) 
     return cell 
} else { 
    return UICollectionViewCell() 
} 


func loadImage(inImageView imageView: PFImageView, withData imageFile: PFFile?) { 
    imageView.image = UIImage(named: "placeholder.png") // Some placeholder while the image loads asyncronously 
    if let imageFile = imageFile { 
     imageView.file = imageFile 
     imageView.loadInBackground(nil) 
    } 
} 
+0

ああ、それはいい考えです。それを指摘していただきありがとうございます。 – CapturedTree

+0

'if let cell'の場合、' cell'はあなたのコードに返されません。 – Larme

+0

答えを編集したことがわかりますが、デキューされたセルはまだ返されません。 – Larme

関連する問題