gpt4 book ai didi

ios - 单击 Collection View 中的单元格即可转换到 View Controller

转载 作者:行者123 更新时间:2023-11-30 11:22:13 24 4
gpt4 key购买 nike

我需要在 Collection View 中单击单元格(仅包含标签)时执行到 View Controller 的转换。我尝试过使用:

collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

以及:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)

但是它们都不起作用,这意味着它们没有被调用。该单元格已设置为启用用户交互并且启用多点触摸。请让我知道如何解决这个问题。

我使用的是 Swift 4.1 和 Xcode 9.4.1。

代码:

   class MyCasesViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

var MyCases : [[String]] = [[]]
var textPassedOver : String?



@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

label.text = textPassedOver



// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Register cell classes
//self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



// MARK: UICollectionViewDataSource

func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return MyCases.count
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return MyCases[0].count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
collectionView.delegate = self
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomViewCell

// Configure the cell

cell.label.text = MyCases[indexPath.section][indexPath.item]

if(indexPath.section == 0){
cell.label.font = UIFont(name: "bold", size: 6)
}else{
cell.label.font = UIFont(name: "HelveticaNeue", size: 14)
}
//cell.isSelected = true

return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "CaseDetails", sender: self )
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print(segue.identifier)
if(segue.identifier == "CaseDetails"){
let destinationVC = segue.destination as! CaseDetailViewController
//CaseDetailViewController.delegate = self
destinationVC.textPassedOver = "pass this text"
}
}

// func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// collectionView.allowsSelection = true
// print("is it here")
// if(indexPath.section == 1 ){
// print("Hurray")
// let viewController = UIViewController()
// self.navigationController?.pushViewController(viewController, animated: true)
// }
// }

// func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexpath indexpath: IndexPath){
// print("is it here now")
// print(indexpath.section)
// print(indexpath.item)
// }
//
// override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// if(segue.identifier == "CaseDetails" ){
// let cell = sender as! CustomViewCell
// var destination = segue.destination as! CaseDetailViewController
// destination.flag = "Hurray"
// }
// }






// Uncomment this method to specify if the specified item should be selected
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}



}

最佳答案

您需要在viewDidLoad中设置委托(delegate)

self.collectionView.delegate = self

关于ios - 单击 Collection View 中的单元格即可转换到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51141726/

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