ios - 查找tableView的哪一行是CollectionView
问题描述
我正在寻求您的帮助,因为我在我的 Swift 应用程序中阻止了某些东西。我想用一些饭菜解析一个数组。我的数组是下一个:
let meals_list: [[String]] = [
["breakfast", "snack_1", "pre_intra_post", "lunch", "snack_2", "diner"],
["breakfast", "snack_1", "lunch", "snack_2", "diner"]
]
如您所见,第一部分有 6 个项目,第二部分有 5 个项目。
这些餐点将显示在一个水平的 CollectionView 中。
每个 CollectionView 都需要在表视图中。
所以,我想在我的第一个收藏视图中创建 6 张卡片,在我的第二个收藏视图中创建 5 张卡片。
这是我管理一切的方式
//Manage meals content
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0{
return 6
} else {
return 5
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! Meal_vc_cell
return cell
}
//Manage day type content (train/rest)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "food_plan_cell", for: indexPath) as! FoodPlanCell
if (indexPath.row == 0){
cell.meals_day_type.text = "Vos repas les\r\njours d'entraînement".uppercased()
} else {
cell.meals_day_type.text = "Vos repas les\r\njours de Repos".uppercased()
}
return cell
}
private func tableView(tableView: UITableView,
willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
guard let tableViewCell = cell as? FoodPlanCell else { return }
tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row)
}
解决方案
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! Meal_vc_cell
cell.meal.text = meals_list.[indexPath.section][indexPath.item]
return cell
}
推荐阅读
- loops - 单行循环内出现错误“'int' object is not iterable”
- search - 如何在 Magento 2.4 中按产品名称获取目录搜索结果?
- encryption - 在 AES-128 中,get 请求参数的密文是否可以包含 16 的非整数倍数的字节数?
- terraform - 如何使用 Terraform 动态块
- c# - Entity Framework Database First - 如何防止运行初始迁移?
- javascript - 如何按日期按 desc 顺序对这个项目数组进行排序(我使用了 loadash sortBy,但它首先返回最旧的,而我需要最新的)?
- java - 即使在 ExpectedConditions.elementToBeClickable 和 visibilityOfElementLocated 条件满足之后,也会引发元素不可交互异常
- javascript - Javascript获取附近的蓝牙设备Mac地址
- javascript - 从回调函数中返回数据
- python - 使用 python 从 azure 函数触发 Azure Devops 管道