skip to Main Content

What I’m meaning is: I have a Table View and Populated Cells, I want each cell, when selected, to link to its on or off state that is represented in its bool value. When it’s in an ‘On’ State, it adds +1 to a counter, when it’s clicked to it’s ‘Off’ state it takes back that count (so subtracting). I have the bool within the class that populates the table view so that it doesn’t mix up selections when filtering data via search bar

Imagine a ‘How many items do you have selected’ type of counter.

What I’m unsure about is how to connect the bool to the selection itself so it’s not ‘mixed up’. Examples are welcome!

Thankyou

2

Answers


  1. To approach this question, I would have a custom table view cell. A great tutorial on this is here: https://www.youtube.com/watch?v=OQYqGM5_wVY&list=LL&index=16&t=752s
    It actually shows the use of a switch in a tableview.

    To have a counter, I would use UINotification Center. In the custom tableview cell swift file, I would add something like the code below to the action for your switch:

    if switch.isOn{
       NotificationCenter.default.post(Notification(name: Notification.Name("tableSwitchClicked"), object: nil, userInfo: ["isOn":true]))
    } else {
       NotificationCenter.default.post(Notification(name: Notification.Name("tableSwitchClicked"), object: nil, userInfo: ["isOn":false]))
    }
    

    "switch" is whatever the outlet name of your switch is.
    Then add this to your viewcontroller swift file in the viewDidLoad:

    NotificationCenter.default.addObserver(self, selector: #selector(changeSwitchCounter(_:)), name: Notification.Name("tableSwitchClicked"), object: nil)
    

    and finally, this in your code after viewDidLoad in the main view controller:

    @objc func changeSwitchCounter(_ notification: Notification){
       let positiveChange = notification.userInfo!["isOn"] as! Bool
       if positiveChange == true {
          switchOnCount += 1
       } else if positiveChange == false {
          switchOnCount -= 1
       }
       
    }
    

    where switchOnCount is the variable you use to track the amount of switches on. I hope this helps you! Please comment if you have any questions.

    Login or Signup to reply.
  2. Propably easiest solution is using simple variable "counter" and count your selections in UITableViewDelegate method didSelectRowAt (counting up) and method didDeselectRowAt (counting down)

    Here are some steps and example code:

    1. create UITableview and conform your UIViewController to UITableViewDelegate protocol . Then add methods to your UIViewController and conform yourTableView.delegate = yourViewController (propably just self).

    Adding delegate methods:

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.counter += 1
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        self.counter -= 1
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search