swift - 点击单元格后在第二个 UI Collection View 中加载特定项目?

标签 swift uicollectionview uicollectionviewcell

我有一个包含不同类别单元格的 Collection View 。当点击其中一个时,我想加载该类别的所有食谱。

我有两个类(class): A。 CategoryModel - 管理类别

    class CategoryModel: NSObject, NSCoding
{
    var nameCategory: String
    var iconCategory: UIImage
    var recipes = [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 中插入某人...但我不知道该怎么做! 请有人帮助我!

RecipeCollView Controller

    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

最佳答案

在下一个屏幕的 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** 访问你的食谱数组

关于swift - 点击单元格后在第二个 UI Collection View 中加载特定项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455719/

相关文章:

ios - 捏缩放后保持 collectionView 单元格位置

ios - 发送消息动画像iMessage发送消息使用UICollectionView自定义

ios - 来自苹果的 APNS 警告,即使我没有在我的应用程序中使用 APNS

javascript - 将 JavaScript 评估到 WKWebView 中以获取视频 URL 不起作用

ios - Firebase queryOrderedByKey 不按时间顺序对帖子进行排序

ios - UICollectionView 滚动问题

swift - 在 UICollectionView 中的第二个索引项处启动图像数组

ios - 如何在 Swift 上将照片库显示为 UICollectionView 或 Assets 网格

xml - 解析 GML 编码的 WFS 响应 XML 文件

swift - macOS Swift 主从委托(delegate)/协议(protocol)不起作用