I have a table view which displays a checkmark for selected items. When I go to the previous view controller then reload my table view controller the selected items previously selected should have checkmarks already. I tried but it is only enabling the last of the selected items. Can anyone help me implement this?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "filterSelectionCell", for: indexPath) as! FilterSelectionCell
activityIndicator.stopAnimating()
activityIndicator.hidesWhenStopped = true
tableDetails.isHidden = false
let product = products[indexPath.row]
cell.brandProductName.text = product.name
cell.accessoryType = product.selected ? .checkmark : .none
if namesArray.count != 0 {
for name in namesArray{
if product.name.contains(name){
print(product.name)
cell.accessoryType = .checkmark
}
else {
cell.accessoryType = .none
}
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selected = products[indexPath.row].selected
products[indexPath.row].selected = !selected
tableView.reloadRows(at: [indexPath], with: .none)
let selectedItems = products.filter{ $0.selected }
let selectedNames = products.filter{ $0.selected }.map{ $0.name }
print(selectedItems)
print(selectedNames)
}
struct Product {
let name : String
let value : String
let img : String
let id : Int
var selected = false
init(dict : [String:Any]) {
self.name = dict["name"] as? String ?? ""
self.value = dict["value"] as? String ?? ""
self.img = dict["img"] as? String ?? ""
self.id = dict["id"] as? Int ?? 0
}
}