将从 func 返回的字典放入 table 视图

Putting a dictionary returned from func into a table view

我有一个函数,它将两个列表放在一起放入字典中。我希望这本字典将每个 [String : String] 打印到我的 table 视图中的一个新单元格中。我只得到一个单元格,我知道为什么(returning 一个项目)。但是我不知道如何访问我的字典列表以 return 字典中的每个项目。 这是我目前得到的图片。 Screenshot of current results

这是我获取项目并放入字典的结构和函数。

//struct for the dictionary that holds all meals
struct RootData: Codable {
    var meals: [MealDetails]
}

//struct for dessert details of each meal
struct MealDetails: Codable {
    var id: String
    var name: String
    var instructions: String?
    var image: String?

    var strIngredient1: String?
    var strIngredient2: String?
    var strIngredient3: String?
    var strIngredient4: String?
    var strIngredient5: String?
    var strIngredient6: String?
    var strIngredient7: String?
    var strIngredient8: String?
    var strIngredient9: String?
    var strIngredient10: String?
    var strIngredient11: String?
    var strIngredient12: String?
    var strIngredient13: String?
    var strIngredient14: String?
    var strIngredient15: String?
    var strIngredient16: String?
    var strIngredient17: String?
    var strIngredient18: String?
    var strIngredient19: String?
    var strIngredient20: String?

    var strMeasure1: String?
    var strMeasure2: String?
    var strMeasure3: String?
    var strMeasure4: String?
    var strMeasure5: String?
    var strMeasure6: String?
    var strMeasure7: String?
    var strMeasure8: String?
    var strMeasure9: String?
    var strMeasure10: String?
    var strMeasure11: String?
    var strMeasure12: String?
    var strMeasure13: String?
    var strMeasure14: String?
    var strMeasure15: String?
    var strMeasure16: String?
    var strMeasure17: String?
    var strMeasure18: String?
    var strMeasure19: String?
    var strMeasure20: String?
    
    enum CodingKeys: String, CodingKey {
        case id = "idMeal"
        case name = "strMeal"
        case instructions = "strInstructions"
        case image = "strMealThumb"
        
        case strIngredient1, strIngredient2, strIngredient3, strIngredient4, strIngredient5, strIngredient6,
             strIngredient7, strIngredient8, strIngredient9, strIngredient10, strIngredient11, strIngredient12,
             strIngredient13, strIngredient14, strIngredient15, strIngredient16, strIngredient17, strIngredient18,
             strIngredient19, strIngredient20
        
        case strMeasure1, strMeasure2, strMeasure3, strMeasure4, strMeasure5, strMeasure6, strMeasure7, strMeasure8, strMeasure9, strMeasure10, strMeasure11, strMeasure12, strMeasure13, strMeasure14, strMeasure15, strMeasure16, strMeasure17, strMeasure18, strMeasure19, strMeasure20
        
    }
    
    func getAllIngredients() -> [String] {
        var ingredients = [String]()
        
        ingredients.append(strIngredient1 ?? "")
        ingredients.append(strIngredient2 ?? "")
        ingredients.append(strIngredient3 ?? "")
        ingredients.append(strIngredient4 ?? "")
        ingredients.append(strIngredient5 ?? "")
        ingredients.append(strIngredient6 ?? "")
        ingredients.append(strIngredient7 ?? "")
        ingredients.append(strIngredient8 ?? "")
        ingredients.append(strIngredient9 ?? "")
        ingredients.append(strIngredient10 ?? "")
        ingredients.append(strIngredient11 ?? "")
        ingredients.append(strIngredient12 ?? "")
        ingredients.append(strIngredient13 ?? "")
        ingredients.append(strIngredient14 ?? "")
        ingredients.append(strIngredient15 ?? "")
        ingredients.append(strIngredient16 ?? "")
        ingredients.append(strIngredient17 ?? "")
        ingredients.append(strIngredient18 ?? "")
        ingredients.append(strIngredient19 ?? "")
        ingredients.append(strIngredient20 ?? "")
        
        return ingredients.filter{![=11=].isEmpty}
        
        
    }
    
    
    func getAllMeasures() -> [String] {
        
        var measures = [String]()
        
        measures.append(strMeasure1 ?? "")
        measures.append(strMeasure2 ?? "")
        measures.append(strMeasure3 ?? "")
        measures.append(strMeasure4 ?? "")
        measures.append(strMeasure5 ?? "")
        measures.append(strMeasure6 ?? "")
        measures.append(strMeasure7 ?? "")
        measures.append(strMeasure8 ?? "")
        measures.append(strMeasure9 ?? "")
        measures.append(strMeasure10 ?? "")
        measures.append(strMeasure11 ?? "")
        measures.append(strMeasure12 ?? "")
        measures.append(strMeasure13 ?? "")
        measures.append(strMeasure14 ?? "")
        measures.append(strMeasure15 ?? "")
        measures.append(strMeasure16 ?? "")
        measures.append(strMeasure17 ?? "")
        measures.append(strMeasure18 ?? "")
        measures.append(strMeasure19 ?? "")
        measures.append(strMeasure20 ?? "")
        
        return measures
        
    }
    
    func getIngredientsAndMeasures() -> [String : String] {
        var dictionary: [String : String] = [:]
        let measures = getAllMeasures()
        for (index, element) in getAllIngredients().enumerated() {
            if !element.isEmpty {
                dictionary[element] = measures[index]
            }
        }
        return dictionary
    }
    
}

这是我的视图控制器:

class DetailTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var idMeal: String = ""
    var mealDetails = [MealDetails]()

    
    //UI Oulets (Image, Dessert Name, Instructions, Ingredents)
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var instructionsText: UITextView!
    @IBOutlet weak var detailTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        getApiDetailData {
            print("data loaded")
            self.view.reloadInputViews()
            self.detailTableView.reloadData()
            
            
            self.nameLabel.text = self.mealDetails[0].name
            self.instructionsText.text = self.mealDetails[0].instructions

            let urlString = (self.mealDetails[0].image)
            let url = URL(string: urlString!)
            self.imageView.downloaded(from: url!)

        }
        detailTableView.delegate = self
        detailTableView.dataSource = self
        
    }

    func tableView(_ detailTableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mealDetails.count
    }
    func tableView(_ detailTableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = "\(mealDetails[indexPath.row].getIngredientsAndMeasures())"
        return cell
    }

我在 numbersOfRowsInSection 函数中尝试了很多方法来访问我的字典。任何帮助将非常感激。我感觉很近,但不确定我忽略了什么。

我认为你应该为每种成分和测量使用自定义类型

struct Ingredient {
    let name: String
    let measurement: String
}

然后通过将此方法添加到结构

,添加一个函数以从 MealDetails 对象创建数组
func createIngredients() -> [Ingredient] {
    return zip(getAllMeasures(), getAllIngredients()).map {
        if [=11=].0.isEmpty { return nil }
        return Ingredient(name: [=11=].0, measurement: [=11=].1)
    }
}

然后你可以在视图控制器中为这个数组设置一个属性

var ingredients = [Ingredient]()

并在 viewDidLoad

的闭包中赋值给它
self.ingredients = self.mealDetails[0].createIngredients()

然后将其用作 table 视图的数据源

func tableView(_ detailTableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ingredients.count
}
func tableView(_ detailTableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    cell.textLabel?.text = "\(ingredients[indexPath.row].name)" // or whatever you want here
    return cell
}