func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
getDataAnswer()
}
func getDataAnswer() {
// Fetch answers
let answerFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Answer")
let answerFilter = questions[AnswerResultPickerView.selectedRow(inComponent: 0)] // **tell me the error**
let answerPredicate = NSPredicate(format: "question == %@", answerFilter)
answerFetch.predicate = answerPredicate
do {
answers = try context.fetch(answerFetch) as! [Answer]
} catch {}
error that appear on the line marked
嗨,我有一个选择器视图(称为AnswerResultPickerView),我希望当用户在第二个组件中选择第一个组件的一行(有问题)时(答案在哪里),答案将根据相关问题。所有数据都在核心数据中,从答案到问题的一对一关系称为“问题”,反之则是一对多
我附上了视图控制器的所有代码,可能会有所帮助:
@IBOutlet weak var AnswerResultPickerView: UIPickerView!
@IBOutlet weak var WeightTextField: UITextField!
@IBOutlet weak var NameTextField: UITextField!
@IBAction func CreateButton(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
self.AnswerResultPickerView.delegate = self
self.AnswerResultPickerView.dataSource = self
}
var questions: [Question] = []
var answers: [Answer] = []
var results: [Result] = []
var data = [[Question](), [Answer](), [Result]()] as [[Any]]
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewWillAppear(_ animated: Bool) {
getDataAnswer()
getData()
AnswerResultPickerView.reloadAllComponents()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let numberOfRowsInComponent0: Int = questions.count
let numberOfRowsInComponent1: Int = answers.count
let numberOfRowsInComponent2: Int = results.count
let returnValue = [numberOfRowsInComponent0, numberOfRowsInComponent1, numberOfRowsInComponent2]
return returnValue[component]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var returnValue = [String]()
switch component {
case 0:
returnValue = [questions[row].text!]
case 1:
returnValue = [answers[row].text!]
case 2:
returnValue = [results[row].tip!]
default:
break
}
return returnValue[0]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
getDataAnswer()
}
func getDataAnswer() {
// Fetch answers
let answerFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Answer")
let answerFilter = questions[AnswerResultPickerView.selectedRow(inComponent: 0)] // here where the error appear
let answerPredicate = NSPredicate(format: "question == %@", answerFilter)
answerFetch.predicate = answerPredicate
do {
answers = try context.fetch(answerFetch) as! [Answer]
} catch {
}
}
func getData() {
// Fetch questions
let questionFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Question")
let questionFilter = addNewMe
let questionPredicate = NSPredicate(format: "me = %@", questionFilter)
questionFetch.predicate = questionPredicate
// Fetch results
let resultFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Result")
let resultFilter = addNewInterest
let resultPredicate = NSPredicate(format: "interest = %@", resultFilter)
resultFetch.predicate = resultPredicate
do {
questions = try context.fetch(questionFetch) as! [Question]
results = try context.fetch(resultFetch) as! [Result]
} catch {
}
}