user6512047
user6512047

Reputation:

how can i store selected rows of tableview in nsuserdefaults in swift 3

I have one tableview in which numbers of rows and in each rows add button and from that i am selecting some and then that selected buttons of rows i want to store in nsuserdefaults in swift 3.

Upvotes: 1

Views: 743

Answers (1)

Lunke
Lunke

Reputation: 151

Saving all the rows that have been selected, can be solved like this:

var selectedRows: [Int] = []

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectedRows.append(indexPath.row)

    UserDefaults.standard.set(selectedRows, forKey: "selectedRows")
}

Later, use

if let selectedRows = UserDefaults.standard.array(forKey: "selectedRows") as? [Int] {
    // Do what you want with the selectedRows
}

if you want to get all of the selectedRows back

Upvotes: 2

Related Questions