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