Wen Love Howard
Wen Love Howard

Reputation: 23

TableViewController is not showing up in simulator

i've created a TableViewController and i've added in my Json data, but the TableView is not showing. My code is working and is not giving any errors, the debugger looks fine as well, but all I see is a white screen. Can anyone tell me what i'm doing wrong?

import UIKit

class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

public var meals = [Meal]()
override func viewDidLoad() {
    self.tableView.delegate = self
    self.tableView.dataSource = self
    super.viewDidLoad()
    fetchJSON {
        self.tableView.reloadData()
 }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  meals.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = UITableViewCell(style: .default , reuseIdentifier: nil)
    cell.textLabel?.text = meals[indexPath.row].strMeal.capitalized
    return cell
}


func fetchJSON(completed: @escaping () -> ()) {
    
    let jsonUrlString =  "https://www.themealdb.com/api/json/v1/1/filter.php?c=Beef"
    
    guard let url = URL(string: jsonUrlString) else {
        print("invalid url string lel")
        return
    }
    print("Select a meal to make today!")
    
    URLSession.shared.dataTask(with: url) { data, response, error in
        guard let data = data else {
            return
        }
        do {
            let decoder = JSONDecoder()
            let mealDetailResponse = try decoder.decode(MealDetailResponse.self, from: data)
            for meals in  mealDetailResponse.meals {
                print("The Meal is \(meals.self)")
                DispatchQueue.main.async {
                    completed()
                }
            }
        }catch {
            print(error)
        }
    }.resume()
}
 

}

Upvotes: 0

Views: 149

Answers (1)

psykocouac
psykocouac

Reputation: 89

I confirm that @RTXGamer solution's works. Also I removed the dispatch from the loop as you want your tableview to be updated only once after all the data have been processed (even if in your case there is only 1 item in mealDetailResponse

    func fetchJSON(completed: @escaping () -> ()) {

        let jsonUrlString =  "https://www.themealdb.com/api/json/v1/1/filter.php?c=Beef"

        guard let url = URL(string: jsonUrlString) else {
            print("invalid url string lel")
            return
        }
        print("Select a meal to make today!")

        URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data else {
                return
            }
            do {
                let decoder = JSONDecoder()
                let mealDetailResponse = try decoder.decode(MealDetailResponse.self, from: data)
                self.meals = mealDetailResponse.meals
                for meals in mealDetailResponse.meals {
                    print("The Meal is \(meals.self)")
                }
                DispatchQueue.main.async {
                    completed()
                }
            }catch {
                print(error)
            }
        }.resume()
    }

Alternatively you can dispatch inside the completion block where you actually need the main thread.

fetchJSON {
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

Upvotes: 1

Related Questions