如何从自定义CollectionViewCell推动VC哪些是在TableViewCell上?

我有一个tableView和单元格,单元格上我有一个collectionView和显示它的一些内容。

我想发送一个selectindexPath的链接。

我想从TableViewCell上的Custom CollectionViewCell推送/呈现我的视图。

 class secondTopicTableViewCell: UITableViewCell { @IBOutlet weak var relatedCustom: UICollectionView! var relArray = NSArray() func loadArray(arr: NSArray) { self.relArray = arr self.relatedCustom.reloadData() } } extension secondTopicTableViewCell : UICollectionViewDataSource { func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return relArray.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collection", forIndexPath: indexPath) as! relatedCollectionViewCell let info = self.relArray.objectAtIndex(indexPath.row) as! specificTopicInfo cell.showInfo(info) return cell } } extension secondTopicTableViewCell : UICollectionViewDelegate { func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let relatedTopic = self.relArray.objectAtIndex(indexPath.row) as! specificTopicInfo let str = relatedTopic.relatedLink! print(str) } } class relatedCollectionViewCell: UICollectionViewCell { @IBOutlet weak var relatedLabel: UILabel! func showInfo(info: specificTopicInfo) { relatedLabel.backgroundColor = UIColor.grayColor() relatedLabel.text = info.relatedTitle } } 

您只需要使用与Tableview控件相同的didSelectItemAtIndexPath进行导航。 将您的导航代码写入CollectionView的didSelectItemAtIndexPath

如果你在一个表视图单元格中嵌套一个集合视图,并想从显示这个第一个单元格的表视图中触发一个动作,你可以做两件事情。

第一

您可以使用UICollectionViewDelegate协议来捕获用户与您的集合视图单元格的交互。

不要忘记将您的表视图单元格设置为其集合视图的委托。

第二

您可以创build自己的协议(当单元格有多个button时很有用)。

然后,每个button将具有自己的“didTap”方法,而不是原始集合视图委托中的“didSelect”方法。