gpt4 book ai didi

ios - 我如何调用 'didselectitematindexpatch' 中的单元格类结果页面

转载 作者:行者123 更新时间:2023-11-28 07:58:00 25 4
gpt4 key购买 nike

我有一个类 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()
}
}

最佳答案

您不能从 Cell 调用 navigationController,您需要从 ViewController 调用。为此,您需要实现委托(delegate)。

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 函数委托(delegate)

小例子:

我有两个 VC,VC A 和 VC B。VC A 有一个 collectionView(因为单元格在类中有一个 collectionView),我想在选择单元格内的单元格时推送 VC B。

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

首先:

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

protocol QuestionsObjectCellDelegate
{
func didSelectItemAtIndex(index:Int)
}

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

第二:

我们需要一个属性,该属性包含具有要触发的事件的对象。此属性名为委托(delegate)。

class QuestionsObjectCell: UICollectionViewCell, ... {

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

}

第三:

选择单元格时调用委托(delegate):

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

第四:

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

class VCA: UIViewController, QuestionsObjectCellDelegate {...}

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

第五名

添加协议(protocol)的功能:

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

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

//The VC A need has navigationController.
}

第六名

在委托(delegate)为 VC A 的 cellForItemAt 集中:

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

=) 这就是全部。

关于ios - 我如何调用 'didselectitematindexpatch' 中的单元格类结果页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47434652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com