I’ve a tableview inside a viewcontroller. The viewcontroller is proven modally. For some purpose it isn’t detecting after I faucet a cell.

On storyboard for the tableview, I’ve choice set to single choice and consumer interplay is enabled. I additionally set the delegate and datasource to the tableview in storyboard. Any concept why it isn’t working?

class RecipeCategoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
        
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return meals.rely
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "recipeCell", for: indexPath) as! RecipeCategoryTableViewCell
        cell.setMeal(textual content: meals[indexPath.row].strMeal)

        return cell
    }
    
    
    @IBOutlet var tableView: UITableView!
        
    var meals = [Meal]()
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        tableView.rowHeight = 300.0
        
        fetchMeals()
    }
    
    override func put together(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "showRecipeDetail" {

           let detailViewController = segue.vacation spot
                as! RecipeDetailViewController

           let myIndexPath = self.tableView.indexPathForSelectedRow!
           let row = myIndexPath.row
           detailViewController.recipeName = meals[row].strMeal
        }
    }
    
    func fetchMeals() {
        let url = URL(string: "https://www.themealdb.com/api/json/v1/1/filter.php?c=Dessert")!
        
        let job = URLSession.shared.dataTask(with: url, completionHandler: { [self] (knowledge, response, error) in
            if let error = error {
                print("Error with fetching meals: (error)")
                
                return
            }
            guard let httpResponse = response as? HTTPURLResponse,
                  (200...299).comprises(httpResponse.statusCode) else {
                print("Error with the response, surprising standing code: (String(describing: response))")
                return
            }
            if let knowledge = knowledge {
                let decodedData = strive? JSONDecoder().decode(MealsModel.self, from: knowledge)
                if let unwrappedData = decodedData {
                    meals = unwrappedData.meals
                    DispatchQueue.foremost.async {
                        self.tableView.reloadData()
                    }
                }
            }
        })
        job.resume()
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("clicked")
        performSegue(withIdentifier: "showRecipeDetail", sender: nil)
    }
}

extension UIImageView {
    func load(url: URL) {
        DispatchQueue.international().async { [weak self] in
            if let knowledge = strive? Knowledge(contentsOf: url) {
                if let picture = UIImage(knowledge: knowledge) {
                    DispatchQueue.foremost.async {
                        self?.picture = picture
                    }
                }
            }
        }
    }
}

I even have a customized cell for the tableview the place I simply join a label and set the textual content from setMeal

outlets

By admin

Leave a Reply

Your email address will not be published.