stakabekor
stakabekor

Reputation: 523

Save didSelectRowAt on Tableview

I want to record the didselectrowat I last closed. I want to save the didselectrowat I chose with userdefaults. When I try to assign data to indexpath, I get the error "Cannot assign to value: 'indexPath' is a 'let' constant'. How do I memorize indexpath?

 var savedata: IndexPath?

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       super.tableView(tableView, didSelectRowAt: indexPath)
   // tableView.deselectRow(at: indexPath, animated: true)
    print("deselectRow", indexPath)
    let save11 = UserDefaults.standard.integer(forKey: "deselectRow")
    savedata = IndexPath(row: save11, section: 0)


    tableView.selectRow(at: savedata, animated: true, scrollPosition: .middle)


    if indexPath == [3,0] {
    UserDefaults.standard.set(60, forKey: "seconds")
        UserDefaults.standard.set(0, forKey: "deselectRow")

        }
     if indexPath == [3,1] {
        UserDefaults.standard.set(900, forKey: "seconds")
        UserDefaults.standard.set(1, forKey: "deselectRow")

    }

Upvotes: 0

Views: 98

Answers (2)

vadian
vadian

Reputation: 285092

It's useless to change the indexPath parameter because it has no effect at all.

To select another row you have to call selectRow(at:animated:scrollPosition:).

Calling super in didSelectRowAt is not mandatory

var savedata: IndexPath?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let save11 = UserDefaults.standard.integer(forKey: "deselectRow")
    savedata = IndexPath(row: save11, section: 0)

    tableView.selectRow(at: savedata, animated: true, scrollPosition: .middle)

    if indexPath.row == 3 {
        UserDefaults.standard.set(60, forKey: "deselectRow")
    }

Upvotes: 1

Adam Wareing
Adam Wareing

Reputation: 246

In swift, let is a constant. Using var will make it a variable and allow you to modify it

Upvotes: 0

Related Questions