简体   繁体   English

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

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

I need to perform a transition to a view controller on the click of a cell (contains only label) in a collection view. 我需要在集合视图中单击单元格(仅包含标签)时执行向视图控制器的过渡。 I have tried using: 我试过使用:

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

as well as: 以及:

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

but none of them work meaning they are not getting invoked. 但是它们都不起作用,意味着它们不会被调用。 The cell has been set to User Interaction enabled and also Multiple touch enabled. 该单元格已设置为“启用用户交互”以及“启用多点触摸”。 Please let me know how to fix this. 请让我知道如何解决此问题。

I am using Swift 4.1 and Xcode 9.4.1. 我正在使用Swift 4.1和Xcode 9.4.1。

Code: 码:

   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

Okay I got it worked.. there is nothing wrong in my code . 好的,我成功了..我的代码没有错。 For a strange reason I found that if I apply constraints to my collectionview the onclick event does not trigger the didselectitemat function. 出于一个奇怪的原因,我发现如果将约束应用于我的collectionview,onclick事件不会触发didselectitemat函数。 When I have removed those constraints it worked. 当我删除这些约束后,它就起作用了。

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

相关问题 将视图 controller 添加到集合视图单元格 - Adding a view controller to a collection view cell 从单元格值更新集合视图控制器 - Updating collection view controller from cell value 在集合视图中选择单元格到新控制器 - Choose cell in collection view to new controller 在集合视图控制器中播放键盘点击声音 - Play keyboard click sound in a collection view controller 视图控制器中的集合视图,单元格触摸集合视图本身的顶部边框(嵌入在导航控制器中) - Collection View in View Controller, Cell touching the top border of Collection View itself (embedded in navigation controller) 如何将数据数据从集合视图单元传递到视图控制器 - how to pass data data from collection view cell to view controller 向左或向右滑动即可将集合视图单元格突出显示加载到视图控制器中 - Swipe left or right to load the view controller with the collection view cell highlight 如何将集合视图单元格的indexPath传递给另一个视图控制器 - How to pass a collection view cell's indexPath to another view controller 表视图控制器中的集合视图单元格(如 Tinder 消息选项卡) - Collection View cell in Table View Controller (like Tinder Message tab) 对每个收集视图单元格使用单个表视图控制器 - use single table view controller for each collection view cell press
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM