首页 > 解决方案 > Swift3 在​​ collectionView 中添加一个按钮 Select / View

问题描述

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

我试着玩

允许多重选择

当用户选择按钮但没有效果时。

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

编辑:添加一些示例代码

这是我尝试过的:

    //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")
    }
}

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

编辑2:

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

    @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")
    }
}

标签: iosswift3uicollectionview

解决方案


BTW 核心基础是

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

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


推荐阅读