我在 ViewController 中有多个 collectionView。这些 collectionViews 的单元格具有相同的格式..所以我正在重用它们。所以我的问题是:如何在方法中识别
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath)
我不想做几个如果
我到处都找到了这个解决方案,但我真的不喜欢它。这是代码
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
if let aCell = cell as? ItemCollectionViewCell{
aCell.setupCell(with: self.items[indexPath.item])
}
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.colletionViewTwo{
// goto viewController1
}else if collectionView == self.colletionViewOne{
// goto viewController2
}
}
最佳答案
创建两个实现 Collection View 委托(delegate)和数据源的类,并分别使用一个。因此,您将在当前的 View Controller 中拥有这两个额外的对象。
现在看到您的代码,上面的代码可能太重了。或者,添加一个字典,将 Collection View 存储为键,将选择器存储为值。这是可扩展的,如您所说。
老实说,您现在遇到的 if
(或 switch
)语句有什么问题?
关于ios - 如果有多个 UICollectionView,则在一个 UICollectionView 中选择了哪个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48252928/