繁体   English   中英

单击集合视图中的单元格即可过渡到视图控制器

[英]Transition to a view controller on click of cell in a collection view

我需要在集合视图中单击单元格(仅包含标签)时执行向视图控制器的过渡。 我试过使用:

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设置委托

self.collectionView.delegate = self

好的,我成功了..我的代码没有错。 出于一个奇怪的原因,我发现如果将约束应用于我的collectionview,onclick事件不会触发didselectitemat函数。 当我删除这些约束后,它就起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM