轻按单元格后在第二个UI集合视图中加载特定项目?

时间:2018-07-21 11:44:02

标签: swift uicollectionview uicollectionviewcell

我有一个具有不同类别单元格的集合视图。点击其中一个,我想加载该类别的所有食谱。

我有两个班级: 一种。 CategoryModel-管理类别

    class CategoryModel: NSObject, NSCoding
{
    var nameCategory: String
    var iconCategory: UIImage
    var recipes = [RecipeModel]()

b。 RecipeModel

    class RecipeModel: NSObject, NSCoding
{
    var nameRecipe: String
    var quantityRecipe: String
    var recipeTime: String
    var preparationTime: String
    var cookingTime: String
    var bakingTempRecipe: String

    var difficultyLevelRecipe: String

    var imageRecipe: UIImage

    var ingredients: [IngredientModel]
    var directions: [DirectionModel]

    var categoryRecipe: String

当我选择所有类别之一时,我想在CategoryCollViewController中插入某人...但是我不知道这样做! 有人帮我吧!

RecipeCollViewcontroller

    class RecipeCollViewController: UICollectionViewController, UITextFieldDelegate
{
    var category: CategoryModel!
    var recipesList = [RecipeModel]()

    struct Storyboard
    {
        static let leftAndRightPaddings: CGFloat = 2.0
        static let numberOfItemsPerRow: CGFloat = 2.0
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()

        longPressGesture()

        RecipeDataManager.shared.recipeController = self

        title = category.nameCategory

        navigationController?.navigationBar.prefersLargeTitles = true

        let collectionViewWidth = collectionView?.frame.width
        let itemWidth = (collectionViewWidth! -  Storyboard.leftAndRightPaddings) / Storyboard.numberOfItemsPerRow

        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWidth, height: 250)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return category.recipesList.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecipeCell", for: indexPath) as! RecipeViewCell
        let recipe = category.recipesList[indexPath.item]

        cell.labelNameRecipe.text = recipe.nameRecipe
        cell.imageViewRecipe.image = recipe.imageRecipe
        cell.labelPrepareTime.text = String(recipe.recipeTimeInt)
        cell.labelQuantityFor.text = recipe.quantityRecipe

        return cell
    }

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
        RecipeDataManager.shared.recipes.remove(at: indexPath.row)
        collectionView.deleteItems(at: [indexPath])
    }

Collection view show the all category Seconc Collection view where I'd like to show the recipes with that category

1 个答案:

答案 0 :(得分:1)

在下一个屏幕的ViewController中声明** recipesList **。

var recipesList = [RecipeModel]()

现在在您的categoryViewController中,实现此CollectionViewDelegate方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let vc=self.storyboard?.instantiateViewController(withIdentifier: "YourViewControllerIdentifier") as? YourViewControllerClass
    recipesList = self.categoryList[indexPath.row].recipes
    self.navigationController?.pushViewController(vc!, animated: true)
}

从先前声明的** var recipesList **访问您的食谱数组