Nitesh Nitesh - 3 months ago 19
Swift Question

UICollectionView in UITableview - Get tag of tapped UICollectionView

I have added

UICollectionView
in
UITableView
. Now, I want to identify on which cell the user taps and the indexPath of that
UICollectionView
.

I'm getting correct
indexPath
but could not get the tag of on which the UICollectionView he tapped. I'm doing something like this to get tap

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


Whole Code

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)
}

}

Answer

You are having multiple section in your tableView, so you can create one NSIndexPath instance in your UserLibraryTableViewCell and initialize it in the cellForRowAtIndexPath like this.

class UserLibraryTableViewCell: UITableViewCell {

    var tableIndexPath: NSIndexPath?

}

Now set this tableIndexPath in the cellForRowAtIndexPath.

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

Now in didSelectItemAtIndexPath of collectionView access tableIndexPath like this.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

     print("\(self.tableIndexPath) ---- \(indexPath.item) ")
}