gpt4 book ai didi

ios - 点击时从 UITableViewCell 内的 UICollectionView 推送到新的 UIViewController

转载 作者:行者123 更新时间:2023-11-28 07:36:41 29 4
gpt4 key购买 nike

目前我有以下代码。问题是。在 didSelectItemAt 上,xCode 告诉我 navigationController (类型“UINavigationController?”没有成员“pushViewController”)

对于以下行:

navigationController?.pushViewController(searchCarViewController, animated: true)

我知道这是正确的,但不知道真正的解决方案。我是在 TableCells 上堆叠 CollectionViews 的新手。

我的树是:

UITableviewController -> UITableViewCell -> UICollectionView(这里我得到点击事件并尝试推送到另一个 ViewController。) -> UICollectionViewCell

class MenuTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var timerTest : Timer?

let menuOptions = [AppMenu(id:"1", name:"Buscar ", imageUrl:"search"),
AppMenu(id:"2", name:"Mis Datos", imageUrl:"datasheet"),
AppMenu(id:"3", name:"Tablas", imageUrl:"table"),
AppMenu(id:"4", name:"Preguntas", imageUrl:"faq")]

var myCollectionView: UICollectionView = {

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
layout.scrollDirection = .vertical

let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.backgroundColor = UIColor.white
view.showsHorizontalScrollIndicator = true
return view
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubview(myCollectionView)

myCollectionView.delegate = self
myCollectionView.dataSource = self
myCollectionView.register(MenuCollectionViewCell.self, forCellWithReuseIdentifier: "collectionCellId")
myCollectionView.translatesAutoresizingMaskIntoConstraints = false
myCollectionView.isScrollEnabled = false
myCollectionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
myCollectionView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
myCollectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
myCollectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return menuOptions.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellId", for: indexPath) as! MenuCollectionViewCell

print(indexPath.row)
let menu = menuOptions[indexPath.row]

cell.nameLabel.text = menu.name
cell.imageView.image = UIImage(named: menu.imageUrl)
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 160, height: 160)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let menu = menuOptions[indexPath.row]
print(menu.name)

let searchCarViewController = VehicleCategoryTableController()
navigationController?.pushViewController(searchCarViewController, animated: true)

}


}

最佳答案

你不能使用

navigationController?.pushViewController(searchCarViewController, animated: true)

在表格单元格内,您需要添加这个

weak var myParent:VCName?

然后在 cellForRowAt 中设置它

cell.myParent = self

那么你可以这样做

myParent?.navigationController?.pushViewController(searchCarViewController, animated: true)

注意你应该有这个层次结构

UINavigationController->UITableviewController -> UITableViewCell -> UICollectionView

另一种更好的方法是让 tableController 像 collection 一样作为 delegate 和 dataSource

cell.collectionView.delegate = self
cell.collectionView.dataSource = self

然后实现里面的方法就可以使用你现在的代码了

关于ios - 点击时从 UITableViewCell 内的 UICollectionView 推送到新的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110001/

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