Swift3 添加按钮 Select / 在 collectionView 中查看

Swift3 adding a button Select / View in collectionView

我正在实现一个显示图像的 collectionView,当用户 select 使用其中一张图像时,它会以大模式打开该图像。 现在我需要在 collectionView 中添加一个按钮,让用户 select 视图模式(如上所述)或 select 模式,用户可以 select 多个项目其他用途。

我试着玩

allowsMultipleSelection

当用户select按下按钮但没有效果时。

我怎样才能实现这样的功能?

编辑:添加一些示例代码

这是我试过的方法:

    //button select mode
    @IBAction func Selection(_ sender: Any) {
    selectMode = !selectMode
    print("select mode is now: \(selectMode)")
    if(selectMode){
        self.navigationItem.title = "Selection de Photos"
        btnSelect.title = "Voir"
        collectionView.allowsMultipleSelection=true 
    }
    else{
        self.navigationItem.title = "Gallerie"
        btnSelect.title = "Selection"
        collectionView.allowsMultipleSelection=false
    }
}

和 didselectitem

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
    if collectionView.allowsMultipleSelection{
        print("multi selection mode activated, no preview")
    } else{
            _selectedCells.add(indexPath)
            collectionView.reloadItems(at: [indexPath])
    }
}


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(!selectMode){
        print("View mode")
        if(segue.identifier as! String == "viewLargePhoto"){
            let controller: ViewPhotoGallerie = segue.destination as! ViewPhotoGallerie
            let indexPath: NSIndexPath = self.collectionView.indexPath(for:sender as! UICollectionViewCell) as! NSIndexPath
            controller.index = indexPath.item
            controller.photosAsset = self.photosAsset
            controller.assetCollection = self.assetCollection
        }
    }
    else{
        print("Select mode")
    }
}

但如果我处于 select 模式(allowsMultipleSelection 为 false),预览 (segue) 启动并且我无法 select 多个项目。

编辑 2:

我已经根据 allowsMultipleSelection 状态更新了我的代码,以大模式查看图像的 segue 仍然打开,然后我无法 select 多个项目。 这是我的代码:

    @IBAction func Selection(_ sender: Any) {
    selectMode = !selectMode
    print("select mode is now: \(selectMode)")
    if(selectMode){
        btnSelect.title = "Voir"
        collectionView.allowsMultipleSelection=true
    }
    else{
        btnSelect.title = "Selection"
        collectionView.allowsMultipleSelection=false
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if(!collectionView.allowsMultipleSelection){
        print("View mode")

        if(segue.identifier as! String == "viewLargePhoto"){
            let controller: ViewPhotoGallerie = segue.destination as! ViewPhotoGallerie
            let indexPath: NSIndexPath = self.collectionView.indexPath(for:sender as! UICollectionViewCell) as! NSIndexPath
            controller.index = indexPath.item
            controller.photosAsset = self.photosAsset
            controller.assetCollection = self.assetCollection
        }
    }
    else{
        print("Select mode")
    }
}

BTW 核心基础是

  1. 您必须存储用户 select 任何单元格上的每个索引,并在用户取消select 该索引时从数组中删除该索引。
  2. 当用户单击按钮时,每次您都需要重新加载该单元格并将按钮状态更新为 selected 或未编辑。
  3. 当用户点击 Submit/Preview 按钮时,您需要使用索引数组来显示 selected 图像。

如果您不清楚该过程,请告诉我。

在选择模式时,您必须要求将 allowsMultipleSelection 设置为 true。

self.collectionView.allowsMultipleSelection = true

要管理选择和预览的不同操作,您可以使用以下条件进行管理

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    if collectionView.allowsMultipleSelection {
        //selection mode
    } else {
        // vide mode
    }
}

完成选择模式后将 allowsMultipleSelection 设置为 false。

self.collectionView.allowsMultipleSelection = false

并找到所选 indexPath 使用。

for indexPath in self.collectionView.indexPathsForSelectedItems! {

}