0
class myChallengesController: UIViewController,UITableViewDelegate, UITableViewDataSource{
   ...
   var myChallenges = [challenge]() //Here i declare an empty array of type challenge
   ...

below is the function i'm using the get all the challenges for a particular user.

func getAllChallenges(){
        var challenges = NSDictionary()
        FIRDatabase.database().reference(withPath: "challenges").observeSingleEvent(of: .value, with: { (snapshot) in
            // getting all of the challenges.

            challenges = (snapshot.value as? NSDictionary)!
            for (_,value) in challenges{
                let test = value as! NSDictionary
                let tempChallenge = challenge()
                for (key,_) in test{
                    // print("key:\(key), value : \(val)")
                    switch(key as! String){

                    case "owner":
                        tempChallenge.owner = test[key] as? String
                        break
                    case "participants":
                        let tempArray = tempChallenge.convertStringToArray(participants: (test[key] as? String)!)
                        tempChallenge.participants = tempArray
                        break
                    case "title":
                        tempChallenge.title = test[key] as? String
                        break
                    case "bounty":
                        tempChallenge.bounty = test[key] as? String
                        break
                    case "details":
                        tempChallenge.details = test[key] as? String
                        break
                    case "location":
                        tempChallenge.location = test[key] as? String
                        break
                    case "subtitle":
                        tempChallenge.subtitle = test[key] as? String
                        break
                    case "duration":
                        tempChallenge.duration = test[key] as? String
                        break
                    case "challengeID":
                        tempChallenge.challengeID = test[key] as? String
                        break
                    default:
                        break
                    }

                }

Here I append each challenge to the myChallenges array. If I test an print data from the first challenge, it works.

                self.myChallenges.append(tempChallenge)
                print("title: \(self.myChallenges[0].title)") -->this works
                print("\(self.myChallenges.count)")
            }
        }) { (error) in
            print(error.localizedDescription)
        }
    }

But when i go and try to access the challenges that were added to the myChallenges array, it appears as if no data was added. Here is where i am accessing the data added to the array.

//this is inside of the tableview cellForRowAt function

cell.jobPosition.text = myChallenges[0].subtitle

cell.timeRemaining.text = myChallenges[0].duration

cell.owner.text = myChallenges[0].owner

when the above code runs i get the "index out range" error

4
  • Is it possible that you're trying to read from the array before it has been populated? I assume since that function is asynchronous, the cell will try to initialise itself on first load (before the data comes back). Commented Jan 21, 2018 at 0:17
  • I think that could be it, how would i correct that? Commented Jan 21, 2018 at 0:17
  • Use myChallenges.first?.subtitle instead. Commented Jan 21, 2018 at 1:13
  • That works, Thanks! but if i want to iterate through multiple items in the array, what should I use? indexPath.row wont work Commented Jan 21, 2018 at 2:12

1 Answer 1

1

You need to provide the UITableView with an appropriate data source, and optionally a delegate. You'll want to do something like this:

// ViewController.viewDidLoad
tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
tableView.dataSource = self

class MyCell: UITableViewCell {
    var jobPosition: UITextField!
    // TODO: configure cell
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
        cell.jobPosition.text = myChallenges[indexPath.row].subtitle
        return cell
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.