我有一个包含不同类别单元格的 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])
}
最佳答案
在下一个屏幕的 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/