UITableview中的UICollectionView – 获取tapped UICollectionView的标记

我在UITableView添加了UICollectionView 。 现在,我想确定用户点击哪个单元格以及该UICollectionView的UICollectionView

我正在获得正确的indexPath但无法获取他所点击的UICollectionView的标记。 我正在做这样的事情来获得点击

  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("\(collectionView.tag) ---- \(indexPath.item) ") } 

整码

 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 return cell } } 

TableViewCell

 class UserLibraryTableViewCell: UITableViewCell { @IBOutlet weak var collectionView: UICollectionView! } 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("\(collectionView.tag) ---- \(indexPath.item) ") } } 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) } } 

您在tableView中有多个部分,因此您可以在UserLibraryTableViewCell创建一个NSIndexPath实例,并在cellForRowAtIndexPath UserLibraryTableViewCell其初始化为这样。

 class UserLibraryTableViewCell: UITableViewCell { var tableIndexPath: NSIndexPath? } 

现在在cellForRowAtIndexPath设置此tableIndexPath

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell cell.tableIndexPath = indexPath return cell } 

现在在collectionView didSelectItemAtIndexPath中访问tableIndexPath就像这样。

 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("\(self.tableIndexPath) ---- \(indexPath.item) ") } 

尝试这个:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell cell.collectionView.tag = indexpath.row return cell } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myBooksGenre.count } 

您没有为表视图的cellForRowAtIndexPath中的特定单元格设置集合视图的Tag

更新代码。

也符合UICollectionViewDelegate协议:

 extension UserLibraryTableViewCell : UICollectionViewDataSource, UICollectionViewDelegate