如何等待数组被填充 Swift
How to wait for an array to be filled in Swift
我正在 Swift 中编写应用程序,但无法正确填充表格视图。
我正在从 Firestore 获取数据,并有一个 class 来帮助我获取该数据。基本过程是我有一个 getProducts 函数,它用产品设置一个本地数组变量。下一步是在我的 tableview class 中创建一个对象数组,但我的 tableview 似乎在我的函数有时间加载到数组中之前构建了一个错误。
所以我的 loadProducts 填充了数组产品,但我的计数似乎是 0。
希望大家帮忙,先谢谢了
我的代码:
class ProductTableViewController: UITableViewController {
var products = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
loadProducts()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return products.count
}
private func loadProducts(){
let dbhelper = DBHelper()
dbhelper.getProducts(){ success in
self.products = dbhelper.products
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ProductTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ProductTableViewCell else {
fatalError("The dequeued cell is not an instance of ProductTableViewCell.")
}
let product = products[indexPath.row]
cell.titleLabel.text = product.Titel
cell.priceLabel.text = product.Prijs
return cell
}
}
在加载产品函数中添加一个 table 查看重新加载语句,以便使用新数据重新加载 table :
private func loadProducts(){
let dbhelper = DBHelper()
dbhelper.getProducts(){ success in
self.products = dbhelper.products
Self.tableView.reloadData()
}
}
我正在 Swift 中编写应用程序,但无法正确填充表格视图。 我正在从 Firestore 获取数据,并有一个 class 来帮助我获取该数据。基本过程是我有一个 getProducts 函数,它用产品设置一个本地数组变量。下一步是在我的 tableview class 中创建一个对象数组,但我的 tableview 似乎在我的函数有时间加载到数组中之前构建了一个错误。 所以我的 loadProducts 填充了数组产品,但我的计数似乎是 0。
希望大家帮忙,先谢谢了
我的代码:
class ProductTableViewController: UITableViewController {
var products = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
loadProducts()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return products.count
}
private func loadProducts(){
let dbhelper = DBHelper()
dbhelper.getProducts(){ success in
self.products = dbhelper.products
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ProductTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ProductTableViewCell else {
fatalError("The dequeued cell is not an instance of ProductTableViewCell.")
}
let product = products[indexPath.row]
cell.titleLabel.text = product.Titel
cell.priceLabel.text = product.Prijs
return cell
}
}
在加载产品函数中添加一个 table 查看重新加载语句,以便使用新数据重新加载 table :
private func loadProducts(){
let dbhelper = DBHelper()
dbhelper.getProducts(){ success in
self.products = dbhelper.products
Self.tableView.reloadData()
}
}