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?