簡體   English   中英

UICollectionView CustomCell重用

[英]UICollectionView CustomCell ReUse

我獲取用戶的Photo Asset ,並試圖創建一個自定義UICollectionViewCellUICollectionView其中第一個指標是Camera Image和其他細胞的攝像機圖像。 但是,當我滾動時,就像重新創建了索引處的圖像,並且我注意到單元格中的某些圖像獲得了在index[0]處添加的視圖。

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
}

下面是用於說明的圖像 錯誤的后繼

當可重用單元在視圖外滾動時,它們會被收集視圖重用,以防止占用大量內存。 當某個單元格滾動到視圖外部時,集合視圖會將它們標記為可重用。 可重用的單元將被集合視圖重用,而不是創建新的單元。 這將大大減少內存使用。 如果只有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

    }

}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM