arrays - 如何在快速附加数组后使表格视图从 Firebase Firestore 加载数据
问题描述
我有这种情况,我从 Firebase 获取数据,将其放入数组中,然后将其填充到我的Table View
. 但是在Table View
函数完成之前加载数据以将数据附加到数组中。因此,我总是Index out of range
出错。
如果你们对我的情况有任何其他解决方案,请随时更正我的代码。但是现在,我只能想办法让函数在Table View
加载数据之前将数据附加到数组中。
希望你们明白我的意思。
这是代码:
import UIKit
import FirebaseFirestore
var arrayCurrentProduct = [currentProduct]()
var arrayRetrievedData: [String:Any] = [:]
var arrayConvertedPrice: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
func initProduct(selectedProduct: String) {
arrayCurrentProduct = referProduct(productName: selectedProduct)
}
private func referProduct(productName: String) -> [currentProduct] {
switch productName {
case Product.Microsoft.rawValue:
getPrices(document: "Msft") { (_) in
self.convertPrice(Type: "std")
self.convertPrice(Type: "dc")
self.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
default:
break
}
return arrayCurrentProduct
}
private func getPrices(document: String, completion: @escaping (_ String:Any?) -> Void) {
retrievePrice(document: document) { [weak self] (data) in
guard let self = self else {return}
self.arrayRetrievedData = data
completion(self.arrayRetrievedData)
}
}
private func convertPrice(Type: String) {
formatterCode(formatter: formatter)
var priceFormatted = ""
guard let price = arrayRetrievedData[Type] else {return}
if let formattedPrice = formatter.string(from: price as! NSNumber) {
priceFormatted = formattedPrice
}
arrayConvertedPrice.append(priceFormatted)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellShowing = tableView.dequeueReusableCell(withIdentifier: ShowingTVCell.cellIdentifier, for: indexPath) as! ShowingTVCell
let cellInput = tableView.dequeueReusableCell(withIdentifier: InputTVCell.cellIdentifier, for: indexPath) as! InputTVCell
switch indexPath.section {
case 0:
cellShowing.labelName.text = microsoft[indexPath.row]
cellShowing.labelPrice.text = arrayConvertedPrice[indexPath.row] // Error index out of range
return cellShowing
case 1:
cellInput.labelSpec.text = microsoft[indexPath.row]
return cellInput
default:
return cellShowing
}
}
这里有一点流程:
我选择了一个产品,initProduct()
称为 -->referProduct()
称为 --> 从 Firebase 获取价格 --> 将价格转换为货币,附加到数组 --> 加载Table View
我也试过reloadData()
在一些地方放,但没有用。任何帮助将不胜感激。
先感谢您。
解决方案
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
您在获取数据之前写了一个幻数 (4)。tableView 尝试在没有数据的情况下为每个部分创建 4 行。您应该在获取数据后更新您的 numbersOfRowsInSection 返回值。
推荐阅读
- delphi - 此操作错误的内存不足 - Delphi 5 和 BDE
- notifications - 是否有关于从迭代中移出的用户故事的报告或通知?
- angular - Angular Material (v8) 模块导入错误
- kotlin - 空检查 lambda 映射以避免 indexOutOfBounds
- post - 在 Kotlin 中使用 Fuel 和 GET 可以,但 POST 不行
- angular - 当我在问题列表中选择下一个单选按钮时,为什么我的单选按钮没有突出显示
- python - 如何修复APscheduler?
- biztalk - IDOC 不是从 SAP 触发但能够在 BizTalk 中看到
- python - Python Django Errno 54 '连接由对等方重置'
- zip - 如何在 Azure 数据工厂 v2 中解压缩 zip 文件