I
I
Ivan Vasilich2018-07-17 23:48:16
Swift
Ivan Vasilich, 2018-07-17 23:48:16

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]
        }
    }
}

if you display the data before the seque, then they will be 0 why I can’t understand if I set a new value in didselectRow

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Vasilich, 2018-07-18
@jcmax

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
}

B
briahas, 2018-07-26
@briahas

Or leave segue and present yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question