1

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?

Cooper Hanson
  • 31
  • 1
  • 7

2 Answers2

2

Use a tag

categoryCollectionview.dataSource = self
categoryCollectionview.tag = indexPath.row

Then

let category = categories[collectionView.tag]
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I tried to implement this into my project, but now when I click on a cell it returns the item from the most recently loaded cell, which is not always the cell I have clicked on – Cooper Hanson Jan 01 '20 at 20:26
0

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)")
}
Cooper Hanson
  • 31
  • 1
  • 7