通过 segue 发送存储在结构中的 JSON 数据
Sending JSON data stored in a struct through a segue
我想在单元格上存储“idMeal”,我单击以通过 segue 将其发送到我的 DetailViewController 并使用它来访问相应膳食的 API 端点。
API with meal details that I'm trying to access for EACH meal when clicked on.
我相信我已经很接近了,但我已经在这里待了几个小时了。
这是我的结构:
//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()
}
}
这是我的详细信息ViewController,我在其中尝试传递“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
}
}
}
我想在单元格上存储“idMeal”,我单击以通过 segue 将其发送到我的 DetailViewController 并使用它来访问相应膳食的 API 端点。
API with meal details that I'm trying to access for EACH meal when clicked on.
我相信我已经很接近了,但我已经在这里待了几个小时了。
这是我的结构:
//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()
}
}
这是我的详细信息ViewController,我在其中尝试传递“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
}
}
}