0

I am running into a problem where I cannot find a solution and searching the web also doesn't help me.

The problem is pretty simple. I know how to make use of TableViews and also how to make use of the numberOfSections function.

The Problem now is, that I am retrieving Data from Firebase Database as an Array of Objects. Here it is members = [PaymentTeam]() where the class looks like this:


class PaymentTeam {
    
    var key: String?
    let memberJob: String?
    let memberName: String?
    let memberID:String?
    let projectCreated:String?
    let projectName: String?
    let confirmedBool: Bool?
    let paidBool: Bool?
        
    
    init(memberJob: String, memberName: String, memberID:String, confirmedBool: Bool, paidBool: Bool, projectCreated:String, projectName: String) {
            

        self.memberName = memberName
        self.memberJob = memberJob
        self.memberID = memberID
        self.projectCreated = projectCreated
        self.confirmedBool = confirmedBool
        self.paidBool = paidBool
        self.projectName = projectName
        }
    
    
    var dictValue: [String: Any] {
        
        return ["memberName" : memberName as Any,
                "memberJob" : memberJob as Any,
                "memberID": memberID as Any,
                "projectCreated": projectCreated as Any,
                "confirmedBool": confirmedBool as Any,
                "paidBool": paidBool as Any,
                "projectName": projectName as Any
                ]
        

    }
    init?(snapshot: DataSnapshot) {
        guard let dict = snapshot.value as? NSDictionary,
            
            let memberJob = dict["memberJob"] as? String,
            let memberName = dict["memberName"] as? String,
            let projectCreated = dict["projectCreated"] as? String,
            let projectName = dict["projectName"] as? String,
            let memberID = dict["memberID"] as? String,
            let confirmedBool = dict["confirmedBool"] as? Bool,
            let paidBool = dict["paidBool"] as? Bool
       
            else {return nil}
                         self.memberJob = memberJob
                         self.memberName = memberName
                         self.projectCreated = projectCreated
                         self.projectName = projectName
                         self.memberID = memberID
                         self.confirmedBool = confirmedBool
                         self.paidBool = paidBool
    }
    
}

and retrieving is done like this:

   static func chosenTeam(for user: User, completion: @escaping ([PaymentTeam]) -> Void) {
        let ref = Database.database().reference().child("selectedTeams").child(User.current.uid)
        ref.observe(DataEventType.value, with: { (snapshot) in
            guard let snapshot = snapshot.children.allObjects as? [DataSnapshot] else {
                return completion([])
            }
            let chosenTeam = snapshot.reversed().compactMap(PaymentTeam.init)
            completion(chosenTeam)
        })
    }

The problem now is, that with:

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return members[section].count
      
        }

I get the error message: Value of type 'PaymentTeam' has no member 'count' and I can only choose between the different class members like members.memberID

For the function:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let member = members[indexPath.section][indexPath.row]
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChosenTeamNameTableViewCell") as! ChosenTeamNameTableViewCell
            cell.memberNameLabel.text = member.memberName
            
            return cell
            }

I get following error message:

Value of type 'PaymentTeam' has no subscripts

Whereby [indexPath.section]is underlined.

Maybe someone can help me to figure out how to structure my Firebase Data in that membersArray to actually make use of numberofSections

Here is the whole code for the ViewController with the Tableview:

class PaymentHomeViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var headerSeperator: UIView!
    
        
   
    var members = [PaymentTeam]()

    override func viewDidLoad() {
            super.viewDidLoad()

            setUpUI()
            
            UserService.chosenTeam(for: User.current) { (chosenTeam) in
                self.members = chosenTeam
                for value in self.members {
                    self.projectTitles.append(value.projectName ?? "Error")
                }
                self.tableView.reloadData()
            }
        }
        

    func setUpUI() {
        Utilities.addShadowtoView(headerSeperator)
    }

 extension PaymentHomeViewController: UITableViewDataSource {

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

          
        }
        
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let label = UILabel()
            label.text = "Header"
            label.backgroundColor = UIColor.lightGray
        }
        
        func numberOfSections(in tableView: UITableView) -> Int {
            return members.count
          }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return members[section].count
      
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            let member = members[indexPath.section][indexPath.row]
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "ChosenTeamNameTableViewCell") as! ChosenTeamNameTableViewCell
            cell.memberNameLabel.text = member.memberName
            
            return cell
            }
        }


    // MARK: - UITableViewDelegate

    extension PaymentHomeViewController: UITableViewDelegate {

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         return 78
            
        }
    }





Thanks for your help in advance. :)

Schaedel420
  • 175
  • 11

1 Answers1

1

In numberOfRowsInSection, you're unintentionally attempting to access a member called "count" of type "PaymentTeam" here:

return members[section].count

"members" is a one-dimensional array, so, members[section] is returning an object of type PaymentTeam, which is not an array and therefore will not have a count. Change it to:

return members.count

Now, your second error arises at this line:

let member = members[indexPath.section][indexPath.row]

Again, "members" is a one-dimensional array, not two-dimensional. You're trying to access a member of a two-dimensional array with array[index1][index2]. This doesn't make sense as PaymentTeam is not an array. You can find some more information about two-dimensional arrays in swift in this StackOverflow thread. Change this to:

let member = members[indexPath.row]
gmdev
  • 2,725
  • 2
  • 13
  • 28