Add switch in UITableView cell in Swift
Asked Answered
F

2

11

How can I embed a UISwitch programmatically in a tableView cell in Swift? I'm doing it like that

let shareLocationSwitch = UISwitch()
cell.accessoryView = shareLocationSwitch
Functionalism answered 31/10, 2017 at 15:9 Comment(4)
it is so simple and easy just create a variable as a UISwitch() and then call cell accesooryView with that you don't need to do many stuff to get there the code is already inside the question.Functionalism
i just edit the question. thank you for your solutions.Functionalism
why my question received -2 point, it was really simple and after i found a good solution i just send it and edit the question so no one get confuse.Functionalism
is there any reason that i don't now about it ?Functionalism
T
39

Here is way you can embed a UISwitch on a UITableView cell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        
                var cell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifire", for: indexPath) as! YourCellClass

                       //here is programatically switch make to the table view 
                        let switchView = UISwitch(frame: .zero)
                        switchView.setOn(false, animated: true)
                        switchView.tag = indexPath.row // for detect which row switch Changed
                        switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged)
                        cell.accessoryView = switchView

               return cell
      }

here is switch call beck method

func switchChanged(_ sender : UISwitch!){

      print("table row switch Changed \(sender.tag)")
      print("The switch is \(sender.isOn ? "ON" : "OFF")")
}

@LeoDabus Great! explanation.

Note: if your tableview may have more than one section then You should create a CustomCell subclassing UITableViewCell and configure your accessoryView inside UITableViewCell awakeFromNib method instead of table view cellForRowAt method. When dequeuing the reusable cell cast it to your CustomCell Here is sample from @LeoDabus

Tocantins answered 31/10, 2017 at 15:13 Comment(10)
anyone explain me please why you give me down voted. please leave a commentTocantins
i accept your answer and did not give you a vote or take any vote from youFunctionalism
Not my down vote but probably because a tableview may have more than one section. You shouldn't use tag property for thisNonjoinder
@LeoDabus Right! I prefer delegate. in this question that is off topic. anyway would you share me for detect each row switch event instant tag propertyTocantins
@NazmulHasan You should create a CustomCell subclassing UITableViewCell and configure your accessoryView inside UITableViewCell awakeFromNib method instead of UITableViewController cellForRowAt method. When dequeuing the reusable cell cast it to your CustomCellNonjoinder
@LeoDabus Great! that's clean code. thank you so much your help.Tocantins
@NazmulHasan dropbox.com/s/l25wk7vq7zsm3zp/TableView%20SwitchSample.zip?dl=1Nonjoinder
I have added sections to the sample so you can use as a reference in the future if needed dropbox.com/s/up4fp9plzyxtlii/…Nonjoinder
Also look at this article it may be useful too . #34971829Functionalism
Why not just use tableview.indexPath(for: sender.superview) in switchChangedBillings
R
0

if you update the array with 3 elements and refreshing the table using self.tableView.reloadData() (for example after 2 second) you can see that with Xcode13 the switch values will be swapped and the example doesn't work. I have similar issue with my App when Xcode has been released to 13. try this: https://drive.google.com/file/d/1_1HuI_JFIYusNmsdyekGBWXsGznneiQ7/view?usp=sharing

Rubirubia answered 8/1, 2022 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.