Search code examples
iosswifttableviewcollectionviewindexpath

Find index of container cell


I am trying to create an online shopping app. Currently, I'm working on a page that displays categories in a tableview. Inside of that tableview there is a collectionview that holds items related to that category. For example: The first cell would say 'Electronics' and then under it would hold a camera in one collectionview cell and a dvd player in the other one. I have gotten to the point where I can create the cells and they populate with the correct data, but I can't figure out how to click on them.

I'm using something like this:

var categoryCollectionview:UICollectionView!
let categories = [Electronics, Shirts, Shoes]
//tableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryContainer", for: indexPath) as! CategoryContainer
    cell.title.text = "Top Sellers in \(categories[indexPath.row].name)"
    categoryCollectionview = cell.categoryCollectionview
    categoryCollectionview.dataSource = self
    categoryCollectionview.delegate = self
}
//collectionView
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    //How can I know what category it's in?
    let category = categories[0]
    let item = category.items[indexPath.row].name

    print("you clicked on \(item)")
}

The problem here is the app can't find what category it's in. I need to turn that 0 to be the indexPath.row of whatever tableView cell it's in. Does anyone know how to do this or know a better way to go about it?


Solution

  • The collection view you are using is the same instance of the collection view class every time, causing the data to overlap. Instead, you should create a new instance each time which will keep the data separated. I also suggest doing the data source and delegate inside the willDisplay function.

    //var categoryCollectionview:UICollectionView!
    //This was your problem ^
    let categories = [Electronics, Shirts, Shoes]
    
    //tableView
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "categoryContainer", for: indexPath) as! CategoryContainer
        cell.title.text = "Top Sellers in \(categories[indexPath.row].name)"
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cell = cell as? CollectionViewCell { //Your custom cell
            let subcategoryView = cell.subcateogyrCollectionView
            subcategoryView?.dataSource = self
            subcategoryView?.delegate = self
            subcateogryView?.tag = indexPath.row
            subcategoryView?.reloadData()
        }
    }
    
    //collectionView
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let category = categories[collectionView.tag]
        let item = category.items[indexPath.row].name
    
        print("you clicked on \(item)")
    }