Я хочу сохранить «idMeal» в ячейке, которую я нажимаю, чтобы отправить ее через переход в мой DetailViewController и использовать ее для доступа к конечной точке API для соответствующего приема пищи.
API с информацией о еде, к которой я пытаюсь получить доступ для КАЖДОГО приема пищи при нажатии.
Я считаю, что я очень близко, но я был здесь в течение нескольких часов.
Вот мои структуры:
//struct for the dictionary holding the desserts
struct DessertDictionary: Codable {
var meals: [Desserts]
}
//struct for all desserts
struct Desserts: Codable {
var strMeal: String?
var strMealThumb: String?
var idMeal: String?
}
//struct for dictionary holding the details
struct MealDataDictionary: Codable {
var meals: IndependentMealData
}
//struct for dessert details
struct IndependentMealData: Codable {
var idMeal: String
var strMeal: String
var strInstructions: String
}
Вот мой первый ViewController:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var mealList = [Desserts]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getApiData {
print("data loaded")
self.tableView.reloadData()
}
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mealList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = "\(mealList[indexPath.row].strMeal!)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailTableViewController {
destination.idMeal = //NEED DATA HERE//
}
}
// MealApi.shared.getApiData(onCompletion: anonFunction)
func getApiData(completed: @escaping () -> ()) {
let url = URL(string: "https://www.themealdb.com/api/json/v1/1/filter.php?c=Dessert")
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
// need to use the TopMeal struct to decode the json because it matches the structure of the json.
let jsonData = try JSONDecoder().decode(DessertDictionary.self, from: data!)
// after you decode the json you access the array within it to setup your array of meals instance variable that is being used by your table view.
self.mealList = jsonData.meals
DispatchQueue.main.async {
completed()
}
}
catch {
print(error)
}
}
}.resume()
}
}
Вот мой DetailViewController, где я пытаюсь передать «idMeal» и использовать для извлечения API для каждой ячейки при нажатии.
class DetailTableViewController: UITableViewController {
var idMeal: String = ""
var mealDatails: IndependentMealData?
//UI Oulets (Image, Dessert Name, Instructions, Ingredents)
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
func getApiDetailData(completed: @escaping () -> ()) {
let urlString = "https://www.themealdb.com/api/json/v1/1/lookup.php?i=\(idMeal)"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
// need to use the TopMeal struct to decode the json because it matches the structure of the json.
let jsonData = try JSONDecoder().decode(MealDataDictionary.self, from: data!)
// after you decode the json you access the array within it to setup your array of meals instance variable that is being used by your table view.
self.mealDatails = jsonData.meals
// print(self.mealDatails?.idMeal)
DispatchQueue.main.async {
completed()
}
}
catch {
print(error)
}
}
}.resume()
}
}
Если вам нужна дополнительная информация, дайте мне знать, я пробовал несколько способов получить доступ к этим данным.





Я должен определить индекс, на который нажал пользователь. Например, просто сохраните строку и используйте ее.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var mealList = [Desserts]()
var indexTapped: Int?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
indexTapped = indexPath.row
performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailTableViewController {
destination.idMeal = mealList[indexTapped!].idMeal
}
}
}
Я ценю, что вы предоставили код, я никогда бы не подумал об этом. Однако это дало эту ошибку Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value Подчеркивание indexTapped!
Как indexTapped хранит номер или индекс ячейки, на которую нажали? Я считаю, что именно поэтому он возвращается к нулю.
@Googler indexTapped = indexPath.row <--- эта строка устанавливает правильное значение
@Googler, кажется, я ошибся. я изменил ответ
Большое спасибо! Вы только что решили мою головную боль в течение дня.
@Googler, так что одобряйте этот ответ
Извини, чувак, только что. Это похоже на мой второй пост здесь, поэтому я не очень хорошо знаком со всем этим.
Просто передайте
idMealкакsenderвместоself