【发布时间】: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