[英]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.