首页 > 解决方案 > 查找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)
    }

标签: iosswiftuitableviewuicollectionview

解决方案


 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
}

推荐阅读