简体   繁体   中英

Swift - I have same data after tapping a cell using table view

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?

First table view with restaurants Second table view with foods
餐厅的第一张桌子视图 食物的第二张桌子视图

HomeTableViewRestaurants collection RestaurantViewController collection

Something like:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    homeTableView.deselectRow(at: indexPath, animated: true)
    let restaurant = filteredData[indexPath.row]
    let vc = RestaurantViewController()
    vc.getDatabaseRecords(forRestaurant: restaurant)
    navigationController?.pushViewController(vc, animated: true)
}

and in second view controller:

func getDatabaseRecords(forRestaurant restaurant : Category) {
        
    food = []
    // Here get the data corresponding to restaurant parameter
    ...
    }

No need to reload tableview as it will done when the table view will appear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM