【发布时间】:2017-11-22 12:23:19
【问题描述】:
我有一个班级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()
}
}
【问题讨论】: