Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 查找容器单元的索引_Ios_Swift_Tableview_Collectionview_Indexpath - Fatal编程技术网

Ios 查找容器单元的索引

Ios 查找容器单元的索引,ios,swift,tableview,collectionview,indexpath,Ios,Swift,Tableview,Collectionview,Indexpath,我正在尝试创建一个在线购物应用程序。目前,我正在处理一个在tableview中显示类别的页面。在该tableview中有一个collectionview,它保存与该类别相关的项。例如:第一个电池上写着“Electronics”,然后在它下面的一个collectionview电池里放着一个摄像头,另一个电池里放着一个dvd播放器。我已经到了可以创建单元格并填充正确数据的地步,但我不知道如何单击它们 我用的是这样的东西: var categoryCollectionview:UICollection

我正在尝试创建一个在线购物应用程序。目前,我正在处理一个在tableview中显示类别的页面。在该tableview中有一个collectionview,它保存与该类别相关的项。例如:第一个电池上写着“Electronics”,然后在它下面的一个collectionview电池里放着一个摄像头,另一个电池里放着一个dvd播放器。我已经到了可以创建单元格并填充正确数据的地步,但我不知道如何单击它们

我用的是这样的东西:

var categoryCollectionview:UICollectionView!
let categories = [Electronics, Shirts, Shoes]
这里的问题是应用程序找不到它所属的类别。我需要将该0转换为它所在的tableView单元格的indexPath.row。有人知道怎么做或者知道更好的方法吗?

使用
标签

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


每次使用的集合视图都是集合视图类的同一实例,导致数据重叠。相反,您应该每次创建一个新实例,以保持数据分离。我还建议在willDisplay函数中执行数据源和委托

//var categoryCollectionview:UICollectionView!
//This was your problem ^
let categories = [Electronics, Shirts, Shoes]

您是否总是在每个类别单元格中显示相同数量的示例产品?不,我不总是显示相同数量的示例项目。通常是两个项目,但在某些情况下只有一个项目返回。您是否考虑过将其类别注入收集单元格,并在选定单元格时读取它?我已经考虑过了,但在实现它时遇到了困难。我尝试在我的项目中实现它,但现在,当我单击一个单元格时,它会返回最近加载的单元格中的项目,而该单元格并不总是我单击过的单元格
categoryCollectionview.dataSource = self
categoryCollectionview.tag = indexPath.row
let category = categories[collectionView.tag]
//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)")
}