2016-01-13 25 views
0

我想在加載UICollectionViewCell中的圖像之前顯示加載圖像。Swift 2 - 在UICollectionViewCell中完成加載圖像時的NSURLConnection調用函數

當我使用NSURLConnectionDelegate加載圖像時,我可以調用一個函數,但是我無法弄清楚一旦加載主圖像時如何隱藏UICollectionViewCell中的圖像。

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 
     let feedCell = cell as! newsFeedCell 

     let pictureURL = NSURL(string: self.newsFeedArray[indexPath.row][1]) 
        feedCell.pictureView.hnk_setImageFromURL(pictureURL!) 
        self.requestNSURLconnection(pictureURL!, cellImage: feedCell) 
    } 


func requestNSURLconnection(url: NSURL, cellImage: newsFeedCell){ 
    cellImage.loadingImage.hidden = false 
    cellImage.pictureView.hidden = true 
    let request: NSURLRequest = NSURLRequest(URL: url) 
    let connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)! 
    connection.start() 


} 


func connectionDidFinishLoading(connection: NSURLConnection!, cellImage: newsFeedCell) 
{ 
    cellImage.loadingImage.hidden = true 
    cellImage.pictureView.hidden = false 
    print("finisched") 
    //loading_view.hidden = true 
} 

如果我不叫'cellImage功能connectionDidFinishLoading打印消息顯示罰款。

我該怎麼做?

回答

0

NSURLConnectionDownloadDelegateconnectionDidFinishLoading簽名是

connectionDidFinishDownloading(_ connection: NSURLConnection, 
       destinationURL destinationURL: NSURL) 

這是不是你得到了什麼。你永遠不會得到你期望的回調。

+0

謝謝。但是,我怎麼能告訴代表來檢查一個單元是否完成了重載? – SNos

+0

最簡單的事情就是讓單元格下載它需要的圖像。 –

相關問題