0

I'm facing the same issue as asked in this question:

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

However, I'm interested in knowing how to repopulate with the saved rows that have been checkmarked?

Thank you!

1 Answer 1

2

Create a variable with didSet so that we can reload table once the value is assigned to it.

var selectedRows: [Int] = [] {
    didSet {
        myTableView.reloadData()
    }
}

On viewDidLoad() assign value to selectedRows from userDefaults

override func viewDidLoad() {
    super.viewDidLoad()        
    selectedRows = UserDefaults.standard.value(forKey: "selectedRows") as? [Int] ?? []
}

Use this code to update cell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "myCell")
    cell.textLabel?.text = "\(indexPath.row)"
    cell.accessoryType = selectedRows.contains(indexPath.row) ? .checkmark : .none
    
    return cell
}

Finally in didSelectRowAt use this logic to update selectedRows variable and store it to userDefaults.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedRows.contains(indexPath.row) {
        self.selectedRows = selectedRows.filter{$0 != indexPath.row}
    }else{
        self.selectedRows.append(indexPath.row)
    }
    UserDefaults.standard.set(selectedRows, forKey: "selectedRows")
}

If you want to store selected index in userdefault on button click, you can use following example.

//
// Within SMViewController.swift file.
//
class SMViewController: UIViewController, UITableViewDataSource {
    
    @IBOutlet weak var myTableView: UITableView!
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: SMCell = tableView.smDequeueReusableCell(forIndexPath: indexPath)
        cell.delegate = self
        return cell
    }
    
}

extension SMViewController: MyCellDelegate {
    func didClickButton(cell: SMCell) {
        if let indexPath = self.myTableView.indexPath(for: cell) {
            // 1) Get all indexes
            var allIndexes = UserDefaults.standard.array(forKey: "myIndexes") as? [Int] ?? []
            // 2) Append current selected index
            allIndexes.append(indexPath.row)
            // 3) Store updated indexes in user defaults.
            UserDefaults.standard.setValue(allIndexes, forKey: "myIndexes")
        }
    }
}

//
// Within SMCell.swift file.
//
protocol MyCellDelegate: AnyObject {
    func didClickButton(cell: SMCell)
}

class SMCell: UITableViewCell {
    weak var delegate: MyCellDelegate?
    @IBOutlet weak var myButton: UIButton!
    
    /// Link this button to your `TableViewCell`
    @IBAction func smButtonHandler(_ sender: UIButton) {
        self.delegate?.didClickButton(cell: self)
    }
}

I hope this is helpful. Let me know if you do have any confusion.

Thanks

8

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.