Answer the question
In order to leave comments, you need to log in
Why do variables return one value in swift?
Why? Why isn't the value written to the variable?
class ListOfQuestions: UITableViewController {
var test_id: Int = 0
var test_row: Int = 0
var listQuestions = [String: AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
// TODO: зделать подсчет ячеек в тесте для автоматизации
let loaded = LoadJson()
if let answers = loaded.loadJson(filename: "answers") {
self.listQuestions = answers // answers["test_id_\(self.test_id)"] // загружаем только ответы указанного теста !!!
}
}
@IBAction func closeIt(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 15
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
cell.textLabel?.text = "Вопрос \(indexPath.row + 1)"
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = indexPath.row + 1
self.test_row = row
print(test_row)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is QuestionsPage {
print("отправляем данные\(test_id) \(test_row)")
let vc = segue.destination as? QuestionsPage
vc?.answerParams = [self.test_id, self.test_row]
}
}
}
Answer the question
In order to leave comments, you need to log in
since prepareForSegue is prepared right after the class is loaded, then you need to catch the pressed cell using
if let path = self.tableView.indexPathForSelectedRow?.row {
self.test_row = path + 1
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question