swift - 在 3 个不同的 CollectionView 中选择特定的单元格以执行对特定视图控制器的 Segue
问题描述
我在一个视图控制器中有 3 个单独的集合视图,每个集合视图中有 3 个项目。我希望用户从每个 collectionView 中选择 1 个单元格,并根据选择的内容选择特定的视图控制器。
我知道我目前正在使用“ If, Else 语句可能涉及一些逻辑。我还使用了 didSelectItemAt 对每个 collectionView 的单元格使用 performSegueWithIdentifier 但是,当用户只在一个 collectionView 中选择 1 个单元格时总是会发生这种情况我没有弄清楚我要去哪里错了。
这是我一直在使用的代码;
class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var collectionViewRed: UICollectionView!
@IBOutlet weak var collectionViewBlue: UICollectionView!
@IBOutlet weak var collectionViewGreen: UICollectionView!
var redColors = ["Red", "Infrared", "Crimson"]
var blueColors = ["Blue", "Light Blue", " Navy Blue"]
var greenColors = ["Green", "Lime Green", "Forest Green"]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt: indexPath: IndexPath) {
if collectionView == self.collectionViewRed {
let cellA = collectionViewRed.cellForItem(at: indexPath) as!
CollectionViewCell
self.performSegue(withIdentifier: "ShowNextView", sender nil)
} else if collectionViewBlue == self.collectionViewBlue {
let cellB = collectionViewBlue.cellForItem(at: indexPath) as!
CollectionViewCell
self.performSegue(withIdentifier: "ShowNextView", sender: nil)
} else if collectionView == self.collectionViewGreen {
let cellC = collectionViewGreen.cellForItem(at: indexPath) as!
CollectionViewCell
}
}
当用户单击 Red CollectionView 中的“Red”时,它执行到 Next ViewController 的 segue,而不是当用户从“redCollectionView”中选择“Red”,然后从“blueCollectionView”中选择“Blue”,最后是“ Green”从“greenCollectionView”执行到特定 ViewController 的 segue。
我应该使用什么代码来允许用户从每个 collectionView 中选择 1 个项目然后执行 segue?
解决方案
您应该有 3 个布尔存储所有集合视图的选定单元格的状态
将这些初始化为 false
var isFirstCollectionViewSelected = false
var isSecondCollectionViewSelected = false
var isThirdCollectionViewSelected = false
// You can also use this for the states, in that case you would not even need the above variables
var firstSelectedIndex: Int?
所以你的didSelectItemAt
会看起来像
func collectionView(_ collectionView: UICollectionView, didSelectItemAt: indexPath: IndexPath) {
if collectionView == self.collectionViewRed {
isFirstCollectionViewSelected = true
self.firstSelectedIndex = indexPath.row
// Add logic here when this cell is selected
} else if collectionView == self.collectionViewBlue {
isSecondCollectionViewSelected = true
// Add logic here when this cell is selected
} else if collectionView == self.collectionViewGreen {
isThirdCollectionViewSelected = true
// Add logic here when this cell is selected
}
performSegueIfAllAreSelected()
}
performSegueIfAllAreSelected()
会在哪里
func performSegueIfAllAreSelected() {
if isFirstCollectionViewSelected, isSecondCollectionViewSelected, isThirdCollectionViewSelected {
if self.firstSelectedIndex == 1 {
self.performSegue(withIdentifier: "ShowNextView", sender: nil)
} else {
//
}
}
}
推荐阅读
- vb.net - 当我在 vb.net 中将它作为参数传递时,如何让 checkbox.checked 事件触发?
- python - 我可以限制numpy可以选择分配给数组的可能dtypes吗
- java - 获取给定范围内的所有完美正方形
- shell - 手动工作正常不工作 cron 语法错误:“(”意外
- php - 如何在 post 方法中优化 if else?
- docker - 无缘无故推送到heroku期间找不到run.sh
- flutter - Flutter 中的某些 API 需要 ui.Image。在某些情况下(当然是用于原型设计),我会使用内置的 Icons.xyz。如何转换为图像?
- angular - 使用按键选择 PrimeNG 表上的行
- arrays - Excel过滤器功能将多个水平过滤器浓缩为1个垂直过滤器
- amazon-web-services - 云看日志洞察查询,结合两个查询