我如何在“didselectitematindexpath”中调用单元格类结果页面

Posted

技术标签:

【中文标题】我如何在“didselectitematindexpath”中调用单元格类结果页面【英文标题】:How can i call in cell class result page in 'didselectitematindexpatch' 【发布时间】:2017-11-22 12:23:19 【问题描述】:

我有一个班级QuestionsObjectCell,我想在点击时打开一个新页面。为此,我创建了另一个类 当我点击时我想调用这个类。我该怎么做?

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

非常感谢。

这是我的单元格类:

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()
    

【问题讨论】:

【参考方案1】:

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

protocol QuestionsObjectCellDelegate

    func didSelectItemAtIndex(index:Int)

您必须在 QuestionsObjectCell 类中添加:

var delegate: QuestionsObjectCellDelegate?

和这个在 didselect 函数中的调用:

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(因为单元格在类中有一个 collectionView),我想在单元格内的一个单元格被选中时推送 VC B。

用不好的话说委托是用来连接事件的。当 X 类中的事件在 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?


第三:

当单元格被选中时调用代理:

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

第四:

在包含单元格的 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集合中,delegate是VC A:

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

=) 这就是全部。

【讨论】:

感谢您的回答 Kevinosaurio。但我是新来的。你能告诉我一个工作的例子吗? 我这里没有我的电脑,但以后我可以用一个小例子来编辑我的答案。同时你可以阅读这篇文章delagates step by step。 谢谢凯维诺萨里奥,我会读的。例如,谢谢。

以上是关于我如何在“didselectitematindexpath”中调用单元格类结果页面的主要内容,如果未能解决你的问题,请参考以下文章

我无法理解如何在 c# 中使用正则表达式或如何解决我的问题

在 jQuery 中,如果元素包含属性,无论其值如何,我如何找到它们? [复制]

我如何在 jwt 令牌中设置过期 30 分钟

如何在pycharm中重新连接我的头?

我如何在 Django 中查询评论

如何在 Codeigniter 中检查是不是设置了会话?