swift - 从 UICollectionView 中删除项目后更新/重新加载 Collection View

标签 swift uicollectionview

我为我的应用程序使用了照片 Collection View 。 我很容易从 Collection View 中删除了项目,但它不会立即从 Collection View 中删除项目。如果我转到另一个 View Controller 并返回到 Collection View Controller ,那么它会更新并从 Collection View 中删除已删除的项目。我希望在发生删除操作时立即更新 Collection View 。我怎么解决这个问题?提前致谢。 我的快速代码如下:

import UIKit
import Photos

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{

var assetCollection: PHAssetCollection = PHAssetCollection()
var photosAsset: PHFetchResult!
var assetThumbnailSize:CGSize!
var selectedIndexPath = [NSIndexPath]()

@IBAction func deleteButton(sender: UIButton) {

    if selectedIndexPath.count != 0{

                PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                    //Delete Photo
                    if let request = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection){
                        for a in self.selectedIndexPath{
                        request.removeAssets([self.photosAsset[a.item]])

                    }
                    }
                    }, completionHandler: nil)}


                self.collectionView.reloadData()
}

这是我的 viewWillAppear 方法:

override func viewWillAppear(animated: Bool) {

    // Get size of the collectionView cell for thumbnail image
    if let layout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout{
        let cellSize = layout.itemSize
        self.assetThumbnailSize = CGSizeMake(cellSize.width, cellSize.height)
    }

    self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)


    if let photoCnt = self.photosAsset?.count{
        if(photoCnt == 0){
            self.noPhotosLabel.hidden = false
        }else{
            self.noPhotosLabel.hidden = true
        }
    }

    self.collectionView.reloadData()

}

最佳答案

您需要在完成处理程序中添加重新加载方法。

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                //Delete Photo
                if let request = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection){
                    for a in self.selectedIndexPath{
                    request.removeAssets([self.photosAsset[a.item]])

                }
                }
                }, completionHandler: { _ in  
  self.photosAsset = PHAsset.fetchAssetsInAssetCollection(self.assetCollection, options: nil)
  dispatch_async(dispatch_get_main_queue(), { self.collectionView.reloadData() }) 
  })

关于swift - 从 UICollectionView 中删除项目后更新/重新加载 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40846708/

相关文章:

swift - 为什么下面的代码会导致无限递归?

iphone - UICollectionView Cell Image 在 GCD 进入 View 时发生变化

ios - 如何在 UICollectionView、Swift 5、Xcode 的行下添加行

swift - 迭代字符串的 UTF8View 时显示的不同类型

swift - 字符串(contentsOfURL :) in Swift 3

ios - 如何通过触摸单元格内的按钮快速编辑文本字段

ios - 将 header 添加到 UICollectionView,而不是 UICollectionView 的部分

ios - 转发 UIScrollViewDelegate 调用时出现问题

ios - UICollectionView 标题部分问题

ios - 使用 Swift 实现 iCloud 键值存储