【问题标题】:Push to new UIViewController from UICollectionView that's inside a UITableViewCell when tapped点击时从位于 UITableViewCell 内的 UICollectionView 推送到新的 UIViewController
【发布时间】:2018-11-01 22:02:17
【问题描述】:

目前我有以下代码。问题是这样的。在 didSelectItemAt xCode 上它告诉我 navigationController (Type 'UINavigationController?' has no member '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)

}


}

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    你不能用

    navigationController?.pushViewController(searchCarViewController, animated: true)
    

    您需要在表格单元格中添加此内容

    weak var myParent:VCName?
    

    然后将其设置在cellForRowAt

    cell.myParent = self
    

    那么你可以这样做

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

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

    UINavigationController->UITableviewController -> UITableViewCell -> UICollectionView

    另一种更好的方法是让tableController作为集合的delegate和dataSource之类的

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多