在过滤器选择视图控制器中,我将选择品牌,然后当我按下应用它将弹出到另一个屏幕,如果用户再次回到同一页面,之前选择的应该有复选标记,并且应该暂时保存,直到用户从过滤页面离开到列表页面
func downloadJsonWithURL() {
let url = URL(string: urlString)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil { print(error!); return }
do {
if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] {
self.products = jsonObj.map{ Product(dict: $0) }
DispatchQueue.main.async {
self.tableDetails.reloadData()
}
}
} catch {
print(error)
}
}
task.resume()
}
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 }
let cell = tableView.cellForRow(at: indexPath)
selectedIndexPathArray.append(indexPath as NSIndexPath)
}
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
}
}