我们希望通过在单击i.e ImageView
a 中的 item ( )时替换主控制器来显示另一个控制器cell collection
。但是我们无法获得主集合视图,也无法导航目标控制器。我们正在使用以下方法 -
HomeViewComcontroller.swift
import LBTAComponents
class HomeViewController: DatasourceController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
collectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0)
collectionView?.backgroundColor = UIColor(r: 232, g: 236, b: 241, a: 1)
let homeViewDatasource = HomeViewDatasource()
self.datasource = homeViewDatasource
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section == 0 {
return CGSize(width: view.frame.width, height: 120)
} else if indexPath.section == 6 {
return CGSize(width: view.frame.width, height: 120)
} else {
return CGSize(width: view.frame.width, height: 200)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 33)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 10)
}
// lazy var homeMainCatgCell: HomeMainCatgCell = {
// let homemain = HomeMainCatgCell ()
// homemain.homeViewcontroller = self
// return homemain
// }()
func handleCtgClick (ctgname: String ,ctgId: String) {
let dummySettingViewController = UIViewController()
dummySettingViewController.view.backgroundColor = UIColor.white
dummySettingViewController.navigationItem.title = ctgname
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController?.pushViewController(dummySettingViewController, animated: true)
}
}
主页查看数据源
import LBTAComponents
class HomeViewDatasource: Datasource {
override func headerClasses() -> [DatasourceCell.Type]? {
return [HomeMainCatgCellHeader.self,HomeSaleCatgCellHeader.self,HomeNewArrivalCellHeader.self,HomeBestSallingCellHeader.self,HomeBigDiscountCellHeader.self,HomeSpecialOfferCellHeader.self,HomeBrandCellHeader.self]
}
override func footerClasses() -> [DatasourceCell.Type]? {
return[HomeCellsFooter.self]
}
override func cellClasses() -> [DatasourceCell.Type] {
return [HomeMainCatgCell.self,HomeSaleCtagCell.self,HomeNewArrivalsCell.self,HomeBestSallingCatgCell.self,HomeBigDiscountCatgCell.self,HomeSpecialOfferCatgCell.self,HomeBrandVIewCell.self]
}
override func numberOfItems(_ section: Int) -> Int {
return 1
}
override func numberOfSections() -> Int {
return 7
}
}
HomeMainCatgCell.swift
import LBTAComponents
class HomeMainCatgCell: DatasourceCell {
var homeViewcontroller: HomeViewController?
let personalcareCatgImageView: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "personalcareimage")
iv.tag = 0
iv.contentMode = .scaleToFill
return iv
}()
let healthcareCatgImageView: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "healthcareimage")
iv.contentMode = .scaleToFill
iv.tag = 1
return iv
}()
let homecareCatgImageView: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "homecareimage")
iv.contentMode = .scaleToFill
iv.tag = 2
return iv
}()
let kitchencareCatgImageView: UIImageView = {
let iv = UIImageView()
iv.image = #imageLiteral(resourceName: "kitchencareiamge")
iv.contentMode = .scaleToFill
iv.tag = 3
return iv
}()
override func setupViews() {
super.setupViews()
let personalcareCatgImageContainerView = UIView()
personalcareCatgImageContainerView.backgroundColor = .white
let homecareCatgImageContainerView = UIView()
homecareCatgImageContainerView.backgroundColor = .white
let healthcareCatgImageContainerView = UIView()
healthcareCatgImageContainerView.backgroundColor = .white
let kitchencareCatgImageContainerView = UIView()
kitchencareCatgImageContainerView.backgroundColor = .white
let imageStackView = UIStackView(arrangedSubviews: [personalcareCatgImageContainerView,homecareCatgImageContainerView,healthcareCatgImageContainerView,kitchencareCatgImageContainerView])
imageStackView.axis = .horizontal
imageStackView.distribution = .fillEqually
addSubview(imageStackView)
imageStackView.anchor(topAnchor, left: leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: frame.width, heightConstant: frame.height)
imageStackView.addSubview(personalcareCatgImageView)
imageStackView.addSubview(healthcareCatgImageView)
imageStackView.addSubview(homecareCatgImageView)
imageStackView.addSubview(kitchencareCatgImageView)
personalcareCatgImageView.anchor(personalcareCatgImageContainerView.topAnchor, left: personalcareCatgImageContainerView.leftAnchor, bottom: personalcareCatgImageContainerView.bottomAnchor, right: personalcareCatgImageContainerView.rightAnchor, topConstant: 5, leftConstant: 5, bottomConstant: 5, rightConstant: 5, widthConstant: personalcareCatgImageContainerView.frame.width , heightConstant: personalcareCatgImageContainerView.frame.width)
healthcareCatgImageView.anchor(healthcareCatgImageContainerView.topAnchor, left: healthcareCatgImageContainerView.leftAnchor, bottom: healthcareCatgImageContainerView.bottomAnchor, right: healthcareCatgImageContainerView.rightAnchor, topConstant: 5, leftConstant: 5, bottomConstant: 5, rightConstant: 5, widthConstant: healthcareCatgImageContainerView.frame.width , heightConstant: healthcareCatgImageContainerView.frame.width)
homecareCatgImageView.anchor(homecareCatgImageContainerView.topAnchor, left: homecareCatgImageContainerView.leftAnchor, bottom: homecareCatgImageContainerView.bottomAnchor, right: homecareCatgImageContainerView.rightAnchor, topConstant: 5, leftConstant: 5, bottomConstant: 5, rightConstant: 5, widthConstant: homecareCatgImageContainerView.frame.width , heightConstant: homecareCatgImageContainerView.frame.width)
kitchencareCatgImageView.anchor(kitchencareCatgImageContainerView.topAnchor, left: kitchencareCatgImageContainerView.leftAnchor, bottom: kitchencareCatgImageContainerView.bottomAnchor, right: kitchencareCatgImageContainerView.rightAnchor, topConstant: 5, leftConstant: 5, bottomConstant: 5, rightConstant: 5, widthConstant: kitchencareCatgImageContainerView.frame.width , heightConstant: kitchencareCatgImageContainerView.frame.width)
personalcareCatgImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClickCtg)))
personalcareCatgImageView.isUserInteractionEnabled = true
healthcareCatgImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClickCtg)))
healthcareCatgImageView.isUserInteractionEnabled = true
homecareCatgImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClickCtg)))
homecareCatgImageView.isUserInteractionEnabled = true
kitchencareCatgImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClickCtg)))
kitchencareCatgImageView.isUserInteractionEnabled = true
}
func handleClickCtg(gestureRecognizer: UITapGestureRecognizer) {
var CtgName: String? = nil
var CtgId: String? = nil
guard let tag = gestureRecognizer.view?.tag else {return}
print(tag)
switch tag {
case 0:
CtgName = "Personal Care"
CtgId = "121"
case 1:
CtgName = "Health Care"
CtgId = "122"
case 2:
CtgName = "Home Care"
CtgId = "123"
case 3:
CtgName = "Kitchen Care"
CtgId = "124"
default:
return
}
self.homeViewController?.handleCtgClick(ctgname: CtgName! ,ctgId: CtgId!)
}
}
主要问题
首先,我没有绑定整个单元格的点击,我只想点击单元格内的 ImageView。因此,为此我将 GestureRecognizer 绑定在图像视图上,例如 -
kitchencareCatgImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClickCtg)))
点击事件在通过print(tag)
内部点击功能打印标签(添加到图像视图中)方面工作正常。但是当调用函数时self.homeViewcontroller.handleCtgClick(ctgname: CtgName! ,ctgId: CtgId!)
(函数存在于HomeViewcontroller
类中)。它不会改变视图控制器。
我还调试handleCtgClick
了函数,发现我得到了HomeViewcontroller
is的对象nil
。
提前致谢