如何从不同的集合视图单元格选择到不同的视图控制器

问题描述 投票:0回答:1

这里的初学者。我正在开发一个简单的灯具应用程序。我已经建立了一个集合视图,我试图做到这一点,因此当我单击集合视图中的单元格时,它将带我到相应的视图控制器。每个单元都应选择到不同的视图控制器。这是我到目前为止尝试过的代码:

import UIKit

class FixturesViewController: UIViewController, UICollectionViewDataSource,       UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


let fixturesName = ["EUMHC 1's XI", "EUMHC 2's XI", "EUMHC 3's XI", "EUMHC 4's XI", "EUMHC 5's XI", "EUMHC 6's XI", "EUMHC 7's XI"]

let fixturesLeague = ["Competing in Men's Premiership & Premier North (BUCS)", "Competing in Men's Regional Division 1 & Scottish 1 (BUCS)", "Competing in Men's Regional Division 2 & Scottish 2 (BUCS)", "Competing in Men's East District Division 1 & Scottish 3 (BUCS)", "Competing in Men's East District Division 2 & Scottish 3 (BUCS)", "Competing in Men's East District Division 2 & Scottish 4 (BUCS)", "Competing in Men's East District Division 4"]


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return fixturesName.count
   }



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! FixturesCollectionViewCell

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    }

    cell.fixturesName.text = fixturesName[indexPath.row]
    cell.fixturesLeague.text = fixturesLeague[indexPath.row]


    cell.contentView.layer.cornerRadius = 10.0
    cell.layer.cornerRadius = 10.0
    cell.contentView.layer.borderWidth = 1.0
    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = true
    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 1.0)
    cell.layer.shadowRadius = 4.0
    cell.layer.masksToBounds = false
    cell.layer.shadowOpacity = 1.0

    return cell


}

private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

switch (indexPath.row)   {
    case 0:
        self.performSegue(withIdentifier: "fixtures1", sender: self)
    case 1:
        self.performSegue(withIdentifier: "fixtures2", sender: self)
  default:
     break
   }
}
}

我已经尝试在底部使用switch语句(在其他地方看到了此方法)来执行带有标识符的搜索,具体取决于单元格的索引。

这是正确的处理方法吗?

swift collections view segue viewcontroller
1个回答
0
投票

如果您有很多话题,那么最好以这种方式构建

self.performSegue(withIdentifier: "fixtures\(indexPath.item + 1)", sender: self)

segue从fixtures1fixturesN开始的位置>

© www.soinside.com 2019 - 2024. All rights reserved.