表格视图单元格中的集合视图
我现在已经在 tableview 单元格中获取了集合视图,当我单击集合视图单元格时,它会转到下一个视图控制器,这可能吗?我已经尝试过选择项目方法,因为如何使用执行转场方法
I have taken collection view in tableview cell now when I click the collection view cell it will go to the next view controller,is it possible? I have tried did select item method in that how to use perform segue method
我给你看例子
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return false
}
}
这个 ViewController 包含 Tableview 数据源 n 个委托.其类型为 DemoTableViewCell 的单元格显示如下
This ViewController contain Tableview datasource n delegates. Its cell of type DemoTableViewCell showing below
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.
Again DemoTableViewCell contain datasouce and delegate of collection view as showing above Also didSelectItemAt contains performSegue, which is using parent refrence for Segue.
现在在集合视图单元格中,有一个名为 parentVC 的变量,您必须像这样在 ViewController 类中定义的 cellForRowatIndexpath 中设置此值
Now in collection view cell, there is 1 variable named parentVC, U have to set this value in cellForRowatIndexpath defined in ViewController class like that
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
}
并最终从情节提要上的 collectionViewCell 转为 YourController
and final make a segue from collectionViewCell on storyboard to YourController