0

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
    }
}
  • When a row is tapped, you need to update your datasource to track the checked / unchecked state. Are you trying to do that already? If so, show that code... if not, that's what you need to do. – DonMag Sep 26 '17 at 12:42
  • i already implemented data source for checked/unchecked state see i had updated my code @DonMag –  Sep 26 '17 at 12:49
  • here i had selected the names and the names are in namesarray but i am unable to place checkmark for the names which are matching @DonMag –  Sep 26 '17 at 12:57
  • you have an array of `products` and you are toggling the `.selected` property... why are you also trying to compare the `.name` property in an array of names? – DonMag Sep 26 '17 at 12:59
  • don't know how to implement correct me if i am wrong i am passing from model class so i was comparing with it @DonMag –  Sep 26 '17 at 13:02
  • ok - a little confusing what you are doing - do you: Have a NavigationController... a table view where you show you product list, and check/uncheck on selection... have a button (or something) that pushes to a new view controller.... then you pop back to the view controller with the table and you want to redisplay the list with the previously selected products still checked. Is that correct? – DonMag Sep 26 '17 at 13:07
  • https://i.stack.imgur.com/gbVAV.png here if i select brand so that it moves to another view controller and there i will select the items as shown in image here https://i.stack.imgur.com/LspEX.png and after selecting apply then it moves to the previous image shown here https://i.stack.imgur.com/vmqqv.png and here i had requirement that if user needs to select more brands then he moves back to and there previously selected should also have check mark and need to add more @DonMag –  Sep 26 '17 at 13:16
  • and what u r guessing is right like as my requirement @DonMag –  Sep 26 '17 at 13:17
  • Ah - so the issue isn't really about the table view, it's about what you're doing with your data. So... you tap "Select fav brand"... new VC shows a list of items... you select one or more, and return to original VC. How are you saving your data? Both for moving between views, and for when the user quits the app and comes back? Are you using Core Data? Something else? – DonMag Sep 26 '17 at 13:23
  • no i just need to save data until the select brand page till disappears and move to main list page when i press apply on top of navigation bar @DonMag –  Sep 26 '17 at 13:25
  • i was just passing data by using protocol to transfer from brands page to select brand page –  Sep 26 '17 at 13:26
  • if i come back to this page https://i.stack.imgur.com/9Mg3M.png from select brand page by pressing apply button then it moves to this page https://i.stack.imgur.com/9Mg3M.png and after coming to this page there is no need of saving previously selected @DonMag –  Sep 26 '17 at 13:29
  • Well, if you want to use your data in multiple areas of your app (even if it's just a couple areas), you have to think about where you are keeping that data, and how you are accessing it. If you just want a "quick fix"... since you are using a protocol to *"transfer from brands page to select brand page"*, then modify that to also pass the selected indexes. Then when you go *to* the brands page, pass into that controller your saved indexes. – DonMag Sep 26 '17 at 13:32
  • that i already tried by didn't success getting lots of errors @DonMag –  Sep 26 '17 at 13:35
  • Take a read through the answers to this post: https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – DonMag Sep 26 '17 at 13:41
  • i tried by using protocol to pass indexpaths back but in that class the array was showing empty @DonMag –  Sep 26 '17 at 13:58
  • 1
    I suggest going through a tutorial or two on how to pass data to / from view controllers. Then create a new project and get a very simple example working. At that point, you should have a good understanding of it, and it will be fairly straight-forward to implement in your full project. – DonMag Sep 26 '17 at 14:20
  • already i am passing the names selected to back view controller successfully and getting data but the indexpaths didn't work as expected @DonMag –  Sep 27 '17 at 04:03
  • Need more information... *"but the indexpaths didn't work as expected"* ... what does that mean? What were you expecting? What was returned? What data do you need that you're not getting? – DonMag Sep 27 '17 at 13:14

0 Answers0