我如何在 'didselectitematindexpatch' 中调用单元格 class 结果页面

How can i call in cell class result page in 'didselectitematindexpatch'

我有一个 class QuestionsObjectCell,我想在点击时打开一个新页面。为此,我创建了另一个 class 我想在单击时将其称为 class。我该怎么做?

为什么 navigationControllerdidselectitematindex 补丁方法中不起作用?我已经研究了好几天了,但还没有得出结论。

非常感谢。

这是我的手机 Class:

import UIKit

class QuestionsObjectCell: UICollectionViewCell, UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    let questionAppCellId = "questionAppCellId"
    let navi = UINavigationController()
    var questionDetailController: QuestionDetailController?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let iconImageView: UIImageView = {
        let icon = UIImageView()
        icon.image = UIImage(named: "function")
        icon.backgroundColor = UIColor.clear
        icon.contentMode = .scaleAspectFill
        icon.translatesAutoresizingMaskIntoConstraints = false
        icon.clipsToBounds = true
        return icon
    }()

    let lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red:0.75, green:0.79, blue:0.81, alpha:1.0)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    let titleText: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 17)
        label.textColor = UIColor(red:0.53, green:0.55, blue:0.63, alpha:1.0)
        label.translatesAutoresizingMaskIntoConstraints = false

        return label
    }()

    let subtitleText: UILabel = {
        let subTitle = UILabel()
        subTitle.text = "12 subtopics can be viewed"
        subTitle.font = UIFont.boldSystemFont(ofSize: 11)
        subTitle.textColor = UIColor(red:0.82, green:0.84, blue:0.86, alpha:1.0)
        subTitle.translatesAutoresizingMaskIntoConstraints = false
        return subTitle
    }()

    let buttonAsk: UIButton = {
        let ask = UIButton(type: .system)
        ask.setTitle("ask", for: .normal)
        ask.setTitleColor(UIColor(red:0.53, green:0.55, blue:0.63, alpha:1.0), for: .normal)
        ask.translatesAutoresizingMaskIntoConstraints = false
        ask.addTarget(self, action: #selector(handleAskButton), for: .touchUpInside)
        return ask
    }()

    func handleAskButton(){
        print("hkasjhdkas")
    }

    let askArrowImageView: UIImageView = {
        let arrow = UIImageView()
        arrow.image = UIImage(named: "next")
        arrow.contentMode = .scaleAspectFill
        arrow.tintColor = UIColor.red
        arrow.translatesAutoresizingMaskIntoConstraints = false
        return arrow
    }()

    func setupViews() {
        addSubview(iconImageView)
        addSubview(lineView)
        addSubview(titleText)
        addSubview(subtitleText)
        addSubview(buttonAsk)
        addSubview(askArrowImageView)

        addConstraintsWidthFormat(format: "H:|[v0]|", views: iconImageView)
        addConstraintsWidthFormat(format: "V:[v0]|", views: iconImageView)

        addConstraintsWidthFormat(format: "H:|[v0]|", views: lineView)
        addConstraintsWidthFormat(format: "V:[v0]|", views: lineView)

        addConstraintsWidthFormat(format: "H:[v0]|", views: titleText)
        addConstraintsWidthFormat(format: "V:|[v0]|", views: titleText)

        addConstraintsWidthFormat(format: "H:[v0]|", views: subtitleText)
        addConstraintsWidthFormat(format: "V:[v0]|", views: subtitleText)

        addConstraints([NSLayoutConstraint(item: iconImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 21)])
        addConstraints([NSLayoutConstraint(item: iconImageView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 16)])
        addConstraints([NSLayoutConstraint(item: iconImageView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0, constant: 34)])
        addConstraints([NSLayoutConstraint(item: iconImageView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 34)])

        addConstraints([NSLayoutConstraint(item: lineView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 19)])
        addConstraints([NSLayoutConstraint(item: lineView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 60)])
        addConstraints([NSLayoutConstraint(item: lineView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0, constant: 1)])
        addConstraints([NSLayoutConstraint(item: lineView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 39)])

        addConstraints([NSLayoutConstraint(item: titleText, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: -10)])
        addConstraints([NSLayoutConstraint(item: titleText, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 66)])

        addConstraints([NSLayoutConstraint(item: subtitleText, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 21)])
        addConstraints([NSLayoutConstraint(item: subtitleText, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 66)])

        addConstraints([NSLayoutConstraint(item: buttonAsk, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 20)])
        addConstraints([NSLayoutConstraint(item: buttonAsk, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: -35)])

        addConstraints([NSLayoutConstraint(item: buttonAsk, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0, constant: 30)])
        addConstraints([NSLayoutConstraint(item: buttonAsk, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 30)])

        addConstraints([NSLayoutConstraint(item: askArrowImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 30)])
        addConstraints([NSLayoutConstraint(item: askArrowImageView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: -15)])
        addConstraints([NSLayoutConstraint(item: askArrowImageView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 0, constant: 11)])
        addConstraints([NSLayoutConstraint(item: askArrowImageView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 11)])

    }

    let listArray = ["Matematik","Geometri","Fizik","Kimya","Biyoloji","Türkçe","Edebiyat","Tarih","Coğrafya","Felsefe","Din Kültürü","Vatandaşlık","Resim","Müzik","Rehberlik","Yabancı Dil","Yazılım Geliştirme","Robotik","Dizayn Tasarım","Fotoğrafçılık","İşletme","Pazarlama","Sağlık Fitness","Kişisel Gelişim"]

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: questionAppCellId, for: indexPath)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: 150, height: 160)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 14, 0, 14)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {      questionDetailController?.showDetailViewController(questionDetailController!, sender: nil)
        print(91827312)
        let nav = UINavigationController()
        let callPage = ResultPage()
        nav.navigationController?.pushViewController(callPage, animated: true)//THIS IS NOT WORK!

    }
}
class ResultPage: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

您不能从 Cell 调用 navigationController,您需要从 ViewController 调用。为此,您需要实施一个委托。

protocol QuestionsObjectCellDelegate
{
    func didSelectItemAtIndex(index:Int)
}

在 QuestionsObjectCell Class 中您必须添加:

var delegate: QuestionsObjectCellDelegate?

并且这个调用在 select 函数中:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.didSelectItemAtIndex(index:indexPath.row)
    }

在VC中你需要设置:

cell.delegate = cell

并创建 didselect 函数委托

小例子:

我有两个 VC、VC A 和 VC B。VC A 有一个 collectionView(因为单元格在 class) 并且当单元格内的单元格被 selected 时,我想推送 VC B。

简而言之委托是用来连接事件的。当 class X 中的事件在 class Y 中触发时,委托的事件也会被触发。

第一个:

我在 QuestionsObjectCell 文件中添加了一个协议:

protocol QuestionsObjectCellDelegate
{
   func didSelectItemAtIndex(index:Int)
} 

class QuestionsObjectCell: UICollectionViewCell, UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { ... }

第二:

我们需要一个 属性,其中包含具有要触发的事件的对象。这个 属性 被命名为委托。

class QuestionsObjectCell: UICollectionViewCell, ... {

  let questionAppCellId = "questionAppCellId"
  let navi = UINavigationController()
  var questionDetailController: QuestionDetailController?
  var delegate: QuestionsObjectCellDelegate?

}

第三个:

单元格为select时调用委托:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
{      
      delegate?.didSelectItemAtIndex(index:indexPath.row)
}

第四个:

在包含我们将协议添加到 class 的单元格的 VC 中。这意味着 VC 现在可以成为我们的协议。

class VCA: UIViewController, QuestionsObjectCellDelegate {...}

将出现警告或错误(Objective C 或 Swift)。此警告指定 vc 没有协议的功能。

第五

添加协议的功能:

func didSelectItemAtIndex(index:Int)
{
    //In your case: 

    let callPage = ResultPage()
    navigationController?.pushViewController(callPage, animated: true)

    //The VC A need has navigationController.  
}

第六

在 cellForItemAt 集中,代表是 VC A:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{  
   //Configure Cell...
   cell.delegate = self
   ...
 }

=) 仅此而已。