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

时间:2018-11-01 22:02:18

标签: ios swift uitableview uicollectionview

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

对于以下行:

navigationController?.pushViewController(searchCarViewController, animated: true)

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

我的树是:

UITableviewController-> UITableViewCell-> UICollectionView (在这里,我获得了tap事件,并尝试推送到另一个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)

}


}

1 个答案:

答案 0 :(得分:0)

您不能使用

navigationController?.pushViewController(searchCarViewController, animated: true)

在表格单元格内部,您需要添加此

weak var myParent:VCName?

然后将其设置在cellForRowAt

cell.myParent = self

那么你可以做到

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

请注意您应该拥有这个层次结构

  

UINavigationController-> UITableviewController-> UITableViewCell-> UICollectionView

另一种更好的方法是使tableController作为集合的委托和数据源,例如

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

然后在其中实现方法,就可以使用当前代码