tableview单元格中的Collectionview

时间:2017-05-22 04:07:26

标签: swift3

我已经在tableview单元格中获取了集合视图,当我单击集合视图单元格时它将转到下一个视图控制器,是否可能?我已经尝试了如何使用执行segue方法

的选项方法

1 个答案:

答案 0 :(得分:1)

我正在展示你的例子

     class ViewController: UIViewController, UITableViewDelegate,
     UITableViewDataSource {
         @IBOutlet weak var tableView: UITableView!  
    override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        return false
    }
 }

此ViewController包含Tableview数据源和委托。它的DemoTableViewCell类型的单元格显示在下面

    class DemoTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
        var parentVC: UIViewController?

        @IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let demoCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DemoCollectionViewCell", for: indexPath) as! DemoCollectionViewCell
        return demoCollectionViewCell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        (self.parentVC as! ViewController).performSegue(withIdentifier: "YourSegueName", sender: self)


    }
    }

DemoTableViewCell再次包含数据库和集合视图的委托,如上所示 还有didSelectItemAt包含performSegue,它使用父级引用Segue。

现在在集合视图单元格中,有1个名为parentVC的变量,U必须在ViewController类中定义的cellForRowatIndexpath中设置此值

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let demoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "DemoTableViewCell", for: indexPath) as! DemoTableViewCell
        demoTableViewCell.parentVC = self
        demoTableViewCell.collectionView.reloadData()
        return demoTableViewCell
    }

并最终从storyboard上的collectionViewCell到YourController

创建一个segue
相关问题