ios - 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")
}
}
解决方案
BTW 核心基础是
- 您必须存储用户选择任何单元格的每个索引,并在用户取消选择该索引时从数组中删除该索引。
- 当用户单击按钮时,每次您需要重新加载该单元格并将按钮状态更新为选中与否。
- 当用户单击
Submit/Preview
按钮时,您需要使用索引数组来显示所选图像。
如果您不清楚程序,请告诉我。
推荐阅读
- elasticsearch - 将日志从 AWS 发送到 Elasticcloud
- java - 如何拒绝输入
- python - 这段代码对 python 线程模块安全吗?
- html - 我是否错误地使用了 flexbox?- 2 列 flexbox 错误
- pre-commit-hook - 是否可以列出受预提交运行影响的文件?
- flutter - 如何在颤振中工作 zonedSchedule 本地通知
- android - 如何使用 Flutter Driver 编写测试以等待关闭应用程序
- javascript - 从命令行控制浏览器 javascript 执行
- c# - 实现 Hangfire.FluentNHibernateStorage 时出现 ORA-00955 错误
- java - 出站 Http Enpoint 的 Spring 集成测试