【发布时间】:2021-12-13 23:30:30
【问题描述】:
您好,我正在尝试使用 Firebase 数据库中的查询结果填充列表
我经常遇到一些问题,我不知道接下来我可以尝试做什么,我通过互联网查看以帮助我做到这一点,但什么也没找到,你能帮帮我吗?
提前致谢
这是有错误的代码
class BoatListViewContoller: UIViewController, UITableViewDelegate, UITableViewDataSource {
var title = [""] // Here error is saying "Property 'title' with type '[String]' cannot override a property with type 'String?'"
var lenght:Int?
func readProducts(){
let db = Firestore.firestore()
db.collection("products").getDocuments(){
querySnapshot, err in
if let err = err {
print("Error getting documents: \(err)")
} else {
self.lenght = querySnapshot!.count
for document in querySnapshot!.documents{
self.title.append(document.data()["title"] as! String) // Here i got a error saying "No exact matches in call to subscript"
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lenght!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
cell.textLabel!.text = title[indexPath]
cell.backgroundColor = UIColor.init(red: 212/255, green: 255/255, blue: 241/255, alpha: 1)
return cell
}
private var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad();
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: displayWidth, height: displayHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
myTableView.backgroundColor = UIColor.init(red: 212/255, green: 255/255, blue: 241/255, alpha: 1)
view.addSubview(myTableView)
}
}
【问题讨论】:
标签: ios swift firebase listview google-cloud-firestore