Search code examples
iosuitableviewcheckboxswift3tableview

How to display saved checklist in tableview swift ios


I want my tableView display saved data. I have tried but failed. I want to keep the checklist after saving and reopening the view.

Please help me for solve my problem.

This my code:

var id = [17,18,19]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellTheme: ThemeCell = self.tableView.dequeueReusableCell(withIdentifier: "cellTheme", for: indexPath) as! ThemeCell

        cellTheme.themeLabel.text = listMenu[indexPath.row].theme
        cellTheme.delegate = self

        if id.isEmpty{

            cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_off")

            print("EMPTY")

        }else{

            for data in id{

                print("TEST THEME \(id)")

                print("DATA ! \(data)")

                if listMenu[indexPath.row].id == data{

                    print("\(listMenu[indexPath.row].id) SAMA DENGAN DATA ")
                    cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_on")

                }else{

                    print("\(listMenu[indexPath.row].id) TIDAK SAMA DENGAN DATA ")
                    cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_off")

                }

            }

        }

        return cellTheme

    }

Note: listMenu[indexPath.row].id = [17, 18, 19, 146]


Solution

  • Just add "break" inside if id == data

    for data in id {
    
         if listMenu[indexPath.row].id == data{
    
               cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_on")
               break
    
           }else{
    
                 cellTheme.checkOutlet.image = UIImage(named: "ic_btn_home_checklist_off")
    
           }
    
    }