【问题标题】:List view populated with firebase query results problem SWIFT IOS填充有 Firebase 查询结果问题 SWIFT IOS 的列表视图
【发布时间】: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


    【解决方案1】:

    第一个错误:

    UIViewController 已经有一个名为title 的属性:

    open var title: String?

    您只需将var title = [""] 重命名为var titles = ["],或与title 不同的名称。

    第二个错误:

    你可以试试document.get("title") as! String借鉴https://stackoverflow.com/a/54601354/3227743的想法),或者你可以试试

    let data = document.data()
    let title = data["title"]
    

    self.title.append((document.data())["title"] as! String)
    

    其他:

    您还需要在解析每个文档后调用myTableView.reloadData()

    Nitpick:length 而不是 lenght。此外,您实际上不需要它,因为您可以(应该)只使用titles.count。

    【讨论】:

    • 非常感谢您提供宝贵的见解,我的数据正在加载(它显示在打印中)但未显示在表格视图中,您认为我现在需要将这个 reloadData() 放在哪里我想知道为什么
    • 您应该在完成解析后,在 for 循环结束后立即执行此操作。
    • 天哪,我的 SWIFT 前辈,它终于可以工作了,我向你鞠躬
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2014-05-11
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多