Deska
Deska

Reputation: 25

List view populated with firebase query results problem SWIFT IOS

Hello I'm trying to do a List that is populated with query results from Firebase Database

I've got constantly some problems and i have no clue what can i try to do next ive looked through internet to help me do this but found nothing, can you help me?

Thanks in Advance

Here is the code with errors

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)
    }
}

Upvotes: 0

Views: 111

Answers (1)

EDUsta
EDUsta

Reputation: 1933

First error:

UIViewController already has a property named title:

open var title: String?

You just need to rename var title = [""] to var titles = ["], or something different than title.

Second error:

You might try document.get("title") as! String (borrowed the idea from https://stackoverflow.com/a/54601354/3227743), or you might try

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

or

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

Miscellaneous:

You would also need to call myTableView.reloadData() after parsing every document.

Nitpick: length instead of lenght. Also, you actually don't need that since you could (should) just use titles.count instead.

Upvotes: 2

Related Questions