我有一个QuestionsObjectCell
类,我想单击时打开一个新页面。为此,我创建了另一堂课我想在单击时打电话给这个课程。我该怎么做?
为什么navigationController
在didselectitematindex
补丁方法中不起作用?我已经研究了几天,但尚未得出结论。
非常感谢。
这是我的单元类:
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()
}
}
您无法从单元格中调用导航controller,您需要从viewController调用。为此,您需要实施一个代表。
protocol QuestionsObjectCellDelegate
{
func didSelectItemAtIndex(index:Int)
}
在质疑类别类中,您必须添加:
var delegate: QuestionsObjectCellDelegate?
和此调用didSelect函数:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.didSelectItemAtIndex(index:indexPath.row)
}
在VC中您需要设置:
cell.delegate = cell
并创建didSelect函数委托
小示例:
i有两个VC,VC A和VCB。VCA具有收集视图(其原因是单元格内部的CollectionView),并且当选择单元格内部单元格时,我想推动VC B。
用可怜的词代表来连接事件。当X类中的事件在类中触发时,也会触发委托的事件。
首先:
我在问题上的文件中添加了一个协议:
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 {...}
将出现警告或错误(目标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
...
}
=)这就是全部。