PerfomSegue在UITableViewCell类中

我有TableViewTableView里面我已经添加了UICollectionView 。 现在,当用户点击任何UICollectionView ,我想传递数据到下一个viewController但不幸的是,我不能使用UITableViewCell类中执行segue,我已经select了项目。 有什么方法可以在这里执行吗?

整个代码

 class UserLibraryViewController: UIViewController { extension UserLibraryViewController : UITableViewDelegate { } extension UserLibraryViewController : UITableViewDataSource { func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return myBooksGenre[section] } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return myBooksGenre.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell cell.tableIndexPath = indexPath return cell } } 

TableViewCell

 class UserLibraryTableViewCell: UITableViewCell { @IBOutlet weak var collectionView: UICollectionView! var tableIndexPath: NSIndexPath? } extension UserLibraryTableViewCell : UICollectionViewDataSource { func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 12 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("userBooksCollectionViewCell", forIndexPath: indexPath) as! UserLibraryCollectionViewCell return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") } } extension UserLibraryTableViewCell : UICollectionViewDelegateFlowLayout { func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let itemsPerRow:CGFloat = 4 let hardCodedPadding:CGFloat = 5 let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) return CGSize(width: itemWidth, height: itemHeight) } } 

创build一个协议,然后在TableViewCell中创build它的实例并在UserLibraryViewController实现该协议,然后在CollectionViewdidSelectItemAtIndexPath中使用该委托实例调用此方法。

 protocol CustomCollectionDelegate { func selectedCollectionCell(indexPath: NSIndexPath) } class UserLibraryTableViewCell: UITableViewCell { @IBOutlet weak var collectionView: UICollectionView! var tableIndexPath: NSIndexPath? var delegate: CustomCollectionDelegate? } 

现在在CollectionViewdidSelectItemAtIndexPath中调用这个方法。

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") self.delegate?.selectedCollectionCell(indexPath) } 

现在在UserLibraryViewController实现这个协议,并在cellForRowAtIndexPath设置委托。

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell cell.tableIndexPath = indexPath cell.delegate = self return cell } extension UserLibraryViewController : CustomCollectionDelegate { func selectedCollectionCell(indexPath: NSIndexPath) { self.performSegueWithIdentifier("Identifier", sender: indexPath) } } 

注意:我已经添加了NSIndexPath作为参数的协议方法,您可以使用自定义对象或Dictionary/Array对其进行更改。