SwiftUI TableViewController help

I’m creating a series of collapsibles by using a ‘UITableViewController’ - however basically I would like the title of the first collapsible to be a piece of the users data which is currently stored in a Firebase database.

My aim is to have something in the following format:

eBay - testebayusername

  • xxxxxx
  • xxxxxx

Here’s the code I currently have:

lass MyProductsViewController: UITableViewController {

var tableViewData = [cellData]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    let db = Firestore.firestore()
           let user = Auth.auth().currentUser

           db.collection("users").getDocuments { (snapshot, error) in
               if let error = error {
                   print(error)
                   return
               } else {
                   for document in snapshot!.documents {
                       let data = document.data()
                       let userId = data["uid"] as! String
                       if userId == user?.uid {
                           let firstName = data["firstname"] as! String
                        let lastName = data["lastname"] as! String
                        let ebayName = data["ebayname"] as! String
                        let depopName = data["depopname"] as! String
    
                       }
                   }
               }
           }

    tableViewData = [cellData(opened: false, title: "EBAY \(ebayName)", sectionData: ["1", "2", "3"])]
    
}

However I am getting the following error - “Cannot find ‘ebayName’ in scope” in the ‘tableViewData’ line. Any ideas why this is happening even though I have declared the ebayName type previously? My list is currently working, I’m just trying to adjust the title naming.

Any help would be greatly appreciated!

Your post title says “SwiftUI TableViewController help” You mean just TableViewController since it appears to be a UIKit (storyboard) project?

is the error occurring on this line:

tableViewData = [cellData(opened: false, title: "EBAY \(ebayName)", sectionData: ["1", "2", "3"])]

Hi Chris, yes - my apologies if it wasn’t clear. That’s the line where I’m getting the error

Variable scope is key to understanding what is going on here.

ebayName is a let constant so it only exists inside if userId == user?.uid { so you would need to assign it to some object inside that if statement scope.

Hi Chris, so basically when a user signs up for an account they are required to provide the following pieces of information:

  • firstname
  • surname
  • eBay account name
  • depot account name

On another page ‘my account’ I have used the above code and it is working absolutely fine, wouldn’t the let ebayName = data["ebayname"] as! String be assigning the ‘ebayName’ object to the users ‘ebayname’ from the database, and when it is called upon in the EBAY \(ebayName) it would then return the data from this field?

I’m happy to send across the tableVC code via a DM if that would help?

What I am saying is the out of scope error is referring to the fact that your line of code:

tableViewData = [cellData(opened: false, title: "EBAY \(ebayName)", sectionData: ["1", "2", "3"])]

is referring to ebayName which is within a different scope in your code. If you moved that line to inside the if statement it will work, BUT that assumes that in that instance there is only going to be ONE document returned from that query. Does that make sense?

ie, like this:

class MyProductsViewController: UITableViewController {

    var tableViewData = [cellData]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let db = Firestore.firestore()
        let user = Auth.auth().currentUser

        db.collection("users").getDocuments { (snapshot, error) in
            if let error = error {
                print(error)
                return
            } else {
                for document in snapshot!.documents {
                    let data = document.data()
                    let userId = data["uid"] as! String
                    if userId == user?.uid {
                        let firstName = data["firstname"] as! String
                        let lastName = data["lastname"] as! String
                        let ebayName = data["ebayname"] as! String
                        let depopName = data["depopname"] as! String

                        tableViewData = [cellData(opened: false, title: "EBAY \(ebayName)", sectionData: ["1", "2", "3"])]
                    }
                }
            }
        }
    }
}
1 Like

Yes I get what you mean, I’ll play around with it and see if I can get it to work. Thanks :slight_smile: