1

I have this table view

I have this table view

this is the code for it

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categorycell", for: indexPath) as! categoryCell
    let recipes = data[indexPath.row]
    cell.Categorylabel.text = recipes.type
    return cell
}

I want to group all the same string in one cell here is some extra info if you need it

struct recipes:Codable{
    var id: Int
    var name: String
    var foodimg: String
    var type: String
    var time: Int
    var HoE: String
    var Servings: Int
    var ingredients:[String]
    var method:[String]
}

here is part of the json file

{
    "id": 1,
    "name": "food",
    "type": "sss",
    "time": 20,
    "HoE": "meduim",
    "Servings": 5,
    "foodimg": "https://ichef.bbci.co.uk/food/ic/food_16x9_1600/recipes/sprout_pasta_44858_16x9.jpg",
    "ingredients": [
      "2 bunches asparagus, woody ends trimmed",
      "2 tsp vinegar",
      "4 fresh eggs",
      "25g (1/3 cup) finely grated parmesan"
    ],
    "method": [
      "Bring a large frying pan of salted water to the boil over medium-high heat. Add the asparagus and cook for 2-3 minutes or until bright green and tender crisp. Use tongs to transfer the asparagus to a plate. Cover to keep warm.",
      "Add the vinegar to the water in pan. Reduce heat to medium-low. Crack 1 egg into a small bowl. Use a spoon to stir the water to make a whirlpool. Carefully pour the egg into the centre of the whirlpool. Cook for 4 minutes for a soft yolk or until cooked to your liking. Use a slotted spoon to transfer to a plate. Cover with foil to keep warm. Repeat with remaining eggs.",
      "Divide the asparagus among serving plates. Top with an egg. Season with salt and pepper. Top with parmesan to serve."
    ]
  },

and I have done this

extension Sequence {
func group<U: Hashable>(by key: (Iterator.Element) -> U) -> [U:[Iterator.Element]] {
    return Dictionary.init(grouping: self, by: key)
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
ahmad12123
  • 11
  • 2

0 Answers0