2015-11-23 33 views
0

我正在提取用戶的Photo Asset並嘗試在我的UICollectionView中創建自定義UICollectionViewCell,其中第一個索引是Camera Image,其他單元格是相機圖像。但是,當我滾動,它像索引圖像重建,我發現在我的細胞一些圖片讓我在index[0]UICollectionView CustomCell ReUse

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    //Deque an GridViewCell 
    let cell: GridViewCell = (collectionView.dequeueReusableCellWithReuseIdentifier(CellReuseIdentifier, forIndexPath: indexPath) as? GridViewCell)! 
    if indexPath.item != 0 { 

     let asset = assetsFetchResults[indexPath.item - 1] as! PHAsset 
     cell.representedAssetIdentifier = asset.localIdentifier 

     imageManager.requestImageForAsset(asset, targetSize: AssetGridThumbnailSize, contentMode: PHImageContentMode.AspectFill, options: nil) { (result: UIImage?, info: [NSObject : AnyObject]?) -> Void in 
      //print(result) 
      if cell.representedAssetIdentifier.isEqualToString(asset.localIdentifier) { 
       if let imageResult = result { 
        cell.imageView.image = imageResult 
       } 
      } 
     } 
    } else { 
     let cameraButton = UIButton(frame: CGRectMake(0,0,80,80)) 
     cameraButton.setTitle("Camera", forState: .Normal) 
     cameraButton.frame = cell.imageView.bounds 
     cell.imageView.addSubview(cameraButton) 
    } 

    return cell 
} 

如下的添加視圖用於說明 ​​

+1

如果單元格的類型不同,那麼您應該使用兩個不同的重用標識符。否則,它會重複使用相同的單元格 –

+0

感謝您的建議@AlokRao –

回答

1

可重複使用的圖像當它們在視圖外滾動時,單元格會被集合視圖重新使用,以防止使用大量內存。當某個單元格在視圖外滾動時,集合視圖會將它們標記爲可重用。可重複使用的單元格將被集合視圖重用,而不是創建新的單元格。這將顯着減少內存使用。如果同時只有20個單元適合屏幕,爲什麼要在內存中保留1000個單元。

由於單元格被重用,它們仍然會包含前一個單元格的內容,這意味着該單元格仍然具有cameraButton作爲子視圖。與圖像一樣的故事,您需要手動從單元格中移除它們,並確保舊的內容被替換或刪除。

當您執行將在一段時間後下載並設置圖像到單元格的方法時,該單元格仍可能包含前一個單元格的圖像。您應該在cellForRowAtIndexPath方法的開頭清除圖像以刪除舊的圖像/子視圖。

見下面的例子:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! GridViewCell 


    if indexPath.row == 0 { 
     // Show the title becouse it's the first cell. 
     cell.titleLabel.hidden = false 

     // Title needs to be set in storyboard 
     // Default image for camera roll needs to be set in storyboard 

    } else { 
     // All the other cells. 
     cell.titleLabel.hidden = true 

     // Clear old content 
     cell.imageView.image = nil 

     // I don't exactly know what all this code does behind the screen, but I assume it's a method that downloads the image and add it to the imageView when it's done. 
     let asset = assetsFetchResults[indexPath.item - 1] as! PHAsset 
     cell.representedAssetIdentifier = asset.localIdentifier 

     imageManager.requestImageForAsset(asset, targetSize: AssetGridThumbnailSize, contentMode: PHImageContentMode.AspectFill, options: nil) { (result: UIImage?, info: [NSObject : AnyObject]?) -> Void in 
      //print(result) 
      if cell.representedAssetIdentifier.isEqualToString(asset.localIdentifier) { 
       if let imageResult = result { 
        cell.imageView.image = imageResult 
       } 
      } 
     } 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    // Cell that needs to open the camera roll when tapped) 
    if indexPath.row == 0 { 

     // Do something that opens the camera roll 

    } 

} 

我沒有測試此代碼,但它應該是與此類似。

+0

我該怎麼做。如果我可以問? –

+0

我改變了我的答案。請讓我知道它對你有用。 –