0

I have a UICollectionView nested inside of a table view cell. How can I get the index path of the horizontal row of the collectionview cell? I tried to use

        let index = cell.tag

        print(index as Any) 

to print which index was being selected not knowing that it will print a value of zero no matter what cell I select. I apologize that I am not experienced with collection views. Any help is much appreciated. Thanks.

ILoveToCode22
  • 235
  • 1
  • 3
  • 12

2 Answers2

0

You should design a protocol handling nested UIElement interactions. It makes your mind clearer and allocate better addresses

class ViewwithTable:UIViewController,UITableViewDataSource,collectionCell_delegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "temp", for: indexPath) as! NestedViewwithCollection
        cell.delegate = self
        return cell
    }

    func didPressed(indexPath: IndexPath) {
        //the pressed index!
    }
}

class NestedViewwithCollection:UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate{
    var delegate:collectionCell_delegate?

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return UICollectionViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let del = delegate{
            del.didPressed(indexPath: indexPath)
        }
    }

}

protocol collectionCell_delegate {
    func didPressed(indexPath:IndexPath)
}
Samson Wong
  • 226
  • 2
  • 10
0

Use this code for tableView

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return model.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    return cell
}

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 
{
    guard let tableViewCell = cell as? TableViewCell else { return }
    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}

Collection View

extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource 
{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return model[collectionView.tag].count
}

func collectionView(collectionView: UICollectionView, 
    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", 
        forIndexPath: indexPath)

    cell.backgroundColor = model[collectionView.tag][indexPath.item]

    return cell
}
Nupur Gupta
  • 305
  • 1
  • 12