gpt4 book ai didi

swift - 如何从不同的 Collection View 单元切换到不同的 View Controller

转载 作者:行者123 更新时间:2023-11-30 10:34:02 25 4
gpt4 key购买 nike

这里是初学者。我正在开发一个简单的装置应用程序。我已经设置了一个 Collection View ,我正在尝试创建它,以便当我单击 Collection View 中的单元格时,它会将我带到相应的 View Controller 。每个单元格应该连接到不同的 View Controller 。这是我迄今为止尝试过的代码:

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 语句(在其他地方看到过此方法)根据单元格的索引使用标识符执行转场。

这是处理问题的正确方法吗?

最佳答案

如果你有很多segues,那么最好这样构建

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

其中segues从fixtures1开始到fixturesN

关于swift - 如何从不同的 Collection View 单元切换到不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com