我需要在集合视图中单击单元格(仅包含标签)时执行到视图控制器的转换。我尝试使用:
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
}
}
答案 0 :(得分:0)
您需要在viewDidLoad
self.collectionView.delegate = self
答案 1 :(得分:0)
好的,我成功了。.我的代码没有错。出于一个奇怪的原因,我发现如果将约束应用于我的collectionview,onclick事件不会触发didselectitemat函数。当我删除这些约束后,它就起作用了。