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
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.