【问题标题】:Find index of container cell查找容器单元格的索引
【发布时间】:2020-04-20 15:24:14
【问题描述】:

我正在尝试创建一个在线购物应用程序。目前,我正在处理一个在表格视图中显示类别的页面。在该 tableview 内部有一个 collectionview 保存与该类别相关的项目。例如:第一个单元格会说“电子产品”,然后在它下面的一个 collectionview 单元格中装有相机,而另一个单元格中装有 dvd 播放器。我已经到了可以创建单元格并填充正确数据的地步,但我不知道如何单击它们。

我正在使用这样的东西:

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)")
}

这里的问题是应用程序找不到它所在的类别。我需要把那个 0 变成它所在的任何 tableView 单元格的 indexPath.row。有谁知道如何做到这一点或知道更好的方法去吧?

【问题讨论】:

  • 您是否总是在每个类别单元格中显示相同数量的示例产品?
  • 不,我并不总是显示相同数量的示例项目。通常是 2 件商品,但在某些情况下只有一件商品返回。
  • 您是否考虑过将其类别注入收集单元格并在选择单元格时读取它?
  • 我已经考虑过了,但我在实施时遇到了麻烦。

标签: ios swift tableview collectionview indexpath


【解决方案1】:

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

//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)")
}

【讨论】:

    【解决方案2】:

    使用tag

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

    然后

    let category = categories[collectionView.tag]
    

    【讨论】:

    • 我试图在我的项目中实现这一点,但是现在当我点击一个单元格时,它会从最近加载的单元格中返回项目,这并不总是我点击过的单元格
    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2019-05-04
    相关资源
    最近更新 更多