我刚创建了一个firebase数据库,其中包含一个问题和4个答案选项以及正确的答案。我正在努力用来自firebase的数据填充选择器视图。我有一些函数显示项目标签中的数据,这是一个问题,但这确实填充了不完整的数据。我需要在pickerview中实现函数来填充答案的选择。我将如何执行此操作,因为当前选择器视图从数组中获取数据。该数组包括一个问题,一个4个答案的选择和一个正确的答案,但我需要从firebase填充选择器视图。
class QuestionsViewController: UIViewController, UIPickerViewDelegate {
@IBOutlet weak var usernamelabel: UILabel! //sets username label
@IBOutlet weak var Next: UIButton! //next button
@IBOutlet weak var itemLabel: UILabel! //item user has selected
@IBOutlet weak var Question: UILabel! //sets question label
@IBOutlet weak var pickerview: UIPickerView! //sets picker view
public var totalQuestions: Int = 0 //sets total question to 0
public var currentQuestion = 0 //sets current question to 0
public var totalCorrect: Int = 0 //sets totalcorrect to 0
var itemSelected: String = "" //item selected
var LabelText = String()
let Exam = Questions() //uses the questions class for instances
var ref: FIRDatabaseReference!
var refHandle: UInt!
override func viewDidLoad() {
super.viewDidLoad() //when the app is loaded
usernamelabel.text = LabelText //username
ref = FIRDatabase.database().reference()
refHandle = ref.observe(.value, with: { (snapshot)in
let dataDict = snapshot.value as! [String: AnyObject]
print (dataDict)
})
itemLabel.text = "" //loads the item label of whats selected
//totalQuestions = quest.count //load the count of the array
// itemSelected = Exam.quiz[currentQuestion][1] //initially when loaded first item is selected
// Question.text = Exam.quiz[currentQuestion][0] //first element in first row of array
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1 //return one component from the picker
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return (Exam.quiz[0].count - 2) //6 columns but only need 4
//array is 6 and minus 2
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
let answers:[String] = Array(Exam.quiz[currentQuestion][1...4]) // which 4 columns to show
return answers[row] //returns the answer
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
let answers:[String] = Array(Exam.quiz[currentQuestion][1...4]) //calculates what the item you selected is called
itemSelected = answers[row]
}
@IBAction func NextAction(_ sender: Any){
ref.child("Questions").child("Q\(currentQuestion)").observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? NSDictionary
let quest = value?["Question"] as? String ?? ""
self.Question.text = quest
// ...
}) { (error) in
print(error.localizedDescription)
}
// currentQuestion = currentQuestion //moves onto next question and increments
if(itemSelected == Exam.quiz[currentQuestion - 1][5]){ //checks if item selection is same as column 6
totalCorrect += 1
itemLabel.text = String(totalCorrect) + "/" + String(totalQuestions)
}
if(currentQuestion < Exam.quiz.count) { // if the current question is lower than the count/size of array
pickerview.reloadAllComponents()
itemSelected = Exam.quiz[currentQuestion][1]
Question.text = Exam.quiz[currentQuestion][0]
} else {
pickerview.isHidden = true
Question.text = "You have finished"
Next.isHidden = true
}
}
}