Learn Courses My Dashboard

Same data in every cell tableview

I have a table view in my restaurant app I’m making. I’m struggling with a problem.I have more restaurants in my app, and I want for every restaurant to display different food, but when I’m selecting any restaurant, I have the same data every where.

I’ll show my code for the first Table View.

This is where I’m declaring the variables (a struct) which I’m using

struct Category {
    let title: String
    let photoKeyHome: String
}
var datas: [Category] = []

var filteredData: [Category]!

override func viewDidLoad() {
    super.viewDidLoad()
    
    filteredData = datas
}

This is where I’m retrieving data from my Firestore (I’m using Firestore and firebase storage).

func getDatabaseRecords() {
    
    let db = Firestore.firestore()
    //  Empty the array
    filteredData = []
    
    db.collection("HomeTableViewRestuarants").getDocuments { (snapshot, error) in
        if let error = error {
            print(error)
            return
        } else {
            for document in snapshot!.documents {
                let data = document.data()
                let newEntry = Category(
                    title: data["title"] as! String,
                    photoKeyHome: data["photoKeyHome"] as! String
                    
                ) 
                
                self.filteredData
                    .append(newEntry)
            }
        }
        DispatchQueue.main.async {
            self.datas = self.filteredData
            self.homeTableView.reloadData()
        }
    }
}

This is my table view and how I’m moving to the second table view where I have the food.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = homeTableView.dequeueReusableCell(withIdentifier: "homeTableViewCell", for: indexPath) as! homeTableViewCell
    let restaurant = filteredData[indexPath.row]
    let storageRef = Storage.storage().reference()
    let photoRef = storageRef.child(restaurant.photoKeyHome)
    cell.myLabel.text = restaurant.title
    cell.myImage.sd_setImage(with: photoRef)
    
    cell.myView.layer.cornerRadius = cell.myView.frame.height / 5
    cell.myImage.layer.cornerRadius = cell.myImage.frame.height / 5
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    homeTableView.deselectRow(at: indexPath, animated: true)
    let vc = RestaurantViewController()
    navigationController?.pushViewController(vc, animated: true)
}

This is how im retrieving data from my second table view (RestaurantViewController)




func getDatabaseRecords() {
            
            let db = Firestore.firestore()
           //  Empty the array
          food = []
            
            db.collection("RestaurantViewController").getDocuments { (snapshot, error) in
                if let error = error {
                    print(error)
                    return
                } else {
                    for document in snapshot!.documents {
                        let data = document.data()
                        let newEntry = Food(photoKeyRestaurant: data["photoKeyRestaurant"] as! String, foodName: data["foodName"] as! String, foodDescription: data["foodDescription"] as! String
                          
                              
                         
                        )
                        print("document is \(document)")
                            
                            
                            
                        self.food.append(newEntry)
                    }
                }
                DispatchQueue.main.async {
                 //  self.datas = self.filteredData
                   self.tableView.reloadData()
                }
            }
        }



This is my struct:

struct Food {
    
    var photoKeyRestaurant: String
    var foodName: String
    var foodDescription: String
}

 var food: [Food] = []

And this is my table view for the restaurant view controller

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return food.count
      
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell", for: indexPath) as! FoodTableViewCell
        let mancare = food[indexPath.row]
        let storageRef = Storage.storage().reference()
        let photoRef = storageRef.child(mancare.photoKeyRestaurant)
        cell.foodImage.sd_setImage(with: photoRef)
        cell.descriptionLabel.text = mancare.foodDescription
        cell.foodNameLabel.text = mancare.foodName
return cell
}

For each restaurant, how can I have different food ?

so by same data you mean it displays the restaurant again? or just lists all the food?

For every restaurant, when I tap on them, I have same food in every restaurant . I want for example when I tap on “Pizzeria Roberto " to have " Pizza Capriciosa”, “Pizza Salami” and when I tap on “Cafe Nostra” to have “Spaghetti carbonara”, “Spaghetti Bolognese”. This is how it looks like.


Imgur: The magic of the Internet this is my food

One way to go about this is to filter your food list based on the name/code of the restaurants before it segues to the food list… This can be done easily during the “prepare” segue method