![](/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.