在协议中发送数组

Sending array in protocol

我正在尝试使用协议将一个数组从 TableView 发送到另一个 TableView,但是当我尝试在第二个 TableView 中显示数据时,我得到了 1元素仅在其中,而不是整个数组。

从模型中,我将 HomeViewExtendedIngredient 数组中的 name 传递到 RecipesDetailsView[=25 中的 TableView 中显示=]

如上图所示,ExtendedIngredient 有 10 个元素,但它只显示 1 个 "all-purpose flour",我想显示所有 10 个项目。

型号:

struct Recipes: Codable {
    let recipes: [Recipe]
}

struct Recipe: Codable {
    let title: String?
    let image: String?
    let pricePerServing: Double?
    let readyInMinutes, servings: Int?
    let instructions: String?
    let extendedIngredients: [ExtendedIngredient]
}

struct ExtendedIngredient: Codable {
    let id: Int?
    let aisle, image: String?
    let name, original, originalString, originalName: String?
    let amount: Double?
    let unit: String?
    let meta, metaInformation: [String]
}

主页视图:

protocol RecipesDetailsSelectActionDelegate: class {
    func recipeDetails(
        recipeTitle: String,
        recipeImage: String,
        recipeInstructions: String,
        ingredientsNumber: String,
        ingredientsNumbersInt: Int,
        ingredientsName: [String]
    )
}

class HomeView: UIView {

    var recipes: Recipes?
    var recipesDetails = [Recipe]()

    weak var recipeDetailsViewSelectActionDelegate: RecipesDetailsSelectActionDelegate?

    override init( frame: CGRect) {
        super.init(frame: frame)
        layoutUI()
    }

lazy var foodTableView: UITableView = {
        let foodTableView = UITableView()
        foodTableView.translatesAutoresizingMaskIntoConstraints = false
        foodTableView.backgroundColor = .customVeryLightGray()
        foodTableView.delegate = self
        foodTableView.dataSource = self
        foodTableView.register(HomeTableViewCell.self, forCellReuseIdentifier: "HomeTableViewCell")
        foodTableView.rowHeight = UITableView.automaticDimension
        foodTableView.estimatedRowHeight = 100
        foodTableView.showsVerticalScrollIndicator = false
        foodTableView.separatorStyle = .none
        return foodTableView
    }()
}

extension HomeView: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        recipeDetailsViewSelectActionDelegate?.recipeDetails(
            recipeTitle: recipesDetails[indexPath.row].title ?? "Error",
            recipeImage: recipesDetails[indexPath.row].image ?? "Error",
            recipeInstructions: recipesDetails[indexPath.row].instructions ?? "Error",
            ingredientsNumber: "\(recipesDetails[indexPath.row].extendedIngredients.count)",
            ingredientsNumbersInt: recipesDetails[indexPath.row].extendedIngredients.count,
            ingredientsName: [(recipesDetails[indexPath.row].extendedIngredients[indexPath.row].name ?? "")]
        )
    }
}

食谱详细信息视图:

class RecipesDetailsView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutUI()
    }
lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 100
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.backgroundColor = .white
        tableView.register(NumberOfIngredientsTableViewCell.self, forCellReuseIdentifier: "NumberOfIngredientsTableViewCell")
        return tableView
    }()
}

extension RecipesDetailsView: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cell = tableView.dequeueReusableCell(withIdentifier: "NumberOfIngredientsTableViewCell", for: indexPath) as! NumberOfIngredientsTableViewCell
            cell.theNameOfIngredient.text = recipeVC.ingredientsName[indexPath.row]
            return cell
        }
}

HomeViewController:

class HomeViewController: UIViewController {

    lazy var mainView: HomeView = {
        let view = HomeView(frame: self.view.frame)
        view.recipeDetailsViewSelectActionDelegate = self
        return view
    }()
}

extension HomeViewController: RecipesDetailsSelectActionDelegate {
    func recipeDetails(recipeTitle: String, recipeImage: String, recipeInstructions: String, ingredientsNumber: String, ingredientsNumbersInt: Int, ingredientsName: [String]) {
        let vc = RecipesDetailsViewController()
        vc.recipeTitle = recipeTitle
        vc.recipeImage = recipeImage
        vc.recipeInstructions = recipeInstructions
        vc.ingredientsNumber = ingredientsNumber
        vc.ingredientsNumberInt = ingredientsNumbersInt
        vc.ingredientsName = ingredientsName
        self.show(vc, sender: nil)
    }
}

RecipesDetailsViewController:

class RecipesDetailsViewController: UIViewController {

    var recipeTitle: String?
    var recipeImage: String?
    var recipeInstructions: String?
    var ingredientsNumber: String?
    var ingredientsNumberInt: Int?
    var ingredientsName: [String] = []

    lazy var mainView: RecipesDetailsView = {
        let view = RecipesDetailsView(frame: self.view.frame)
        view.backgroundColor = .white
        return view
    }()

    override func loadView() {
        super.loadView()
        mainView.recipeVC = self
        view = mainView
    }
}

问题出在下面一行: 成分名称:[(食谱详情[indexPath.row].extendedIngredients[indexPath.row].name ?? "")]

"extendedIngredients[indexPath.row]" 此表达式将始终 return 仅一个对象,具体取决于所选行。

您可能需要执行以下操作: (食谱详情[indexPath.row].extendedIngredients.compactMap({$0.name}))