Я пытаюсь отправить массив из TableView
в другой TableView
, используя протокол, но когда я пытаюсь показать данные во втором TableView
, я получаю только 1 элемент, а не весь массив.
Из модели я передаю name
в ExtendedIngredient
массив из HomeView
для отображения в TableView
в RecipesDetailsView
Как и на рисунке выше, ExtendedIngredient
имеет 10 элементов, но показывает только 1 «универсальную муку», и я хочу показать все 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]
}
HomeView:
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 ?? "")]
)
}
}
RecipesDetailsView:
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
}
}