![](/img/trans.png)
[英]How to reload data in table view by tapping the cell of collection view in swift
[英]Swift - I have same data after tapping a cell using table view
我正在制作的餐廳應用程序中有一個表格視圖。 我正在努力解決一個問題。我的應用程序中有更多餐廳,我希望每家餐廳都顯示不同的食物,但是當我選擇任何一家餐廳時,我到處都有相同的數據。
我將展示我的第一個表視圖的代碼。
這是我聲明我正在使用的變量(結構)的地方
struct Category {
let title: String
let photoKeyHome: String
}
var datas: [Category] = []
var filteredData: [Category]!
override func viewDidLoad() {
super.viewDidLoad()
filteredData = datas
}
這是我從 Firestore 中檢索數據的地方(我正在使用 Firestore 和 firebase 存儲)。
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()
}
}
}
這是我的表格視圖,以及我如何移動到我有食物的第二個表格視圖。
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)
}
這就是我從我的第二個表視圖(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()
}
}
}
這是我的結構:
struct Food {
var photoKeyRestaurant: String
var foodName: String
var foodDescription: String
}
var food: [Food] = []
這是我的餐廳視圖 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
}
對於每家餐廳,我怎樣才能有不同的食物?
餐廳的第一張桌子視圖 | 食物的第二張桌子視圖 |
---|---|
就像是:
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)
}
在第二個視圖中 controller:
func getDatabaseRecords(forRestaurant restaurant : Category) {
food = []
// Here get the data corresponding to restaurant parameter
...
}
無需重新加載表格視圖,因為它會在表格視圖出現時完成
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.