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
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:
"switch" is whatever the outlet name of your switch is.
Then add this to your viewcontroller swift file in the viewDidLoad:
and finally, this in your code after viewDidLoad in the main view controller:
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.
Propably easiest solution is using simple variable "counter" and count your selections in
UITableViewDelegate
methoddidSelectRowAt
(counting up) and methoddidDeselectRowAt
(counting down)Here are some steps and example code:
UITableview
and conform yourUIViewController
toUITableViewDelegate
protocol . Then add methods to yourUIViewController
and conformyourTableView.delegate = yourViewController
(propably just self).Adding delegate methods: