Jigen Jigen - 11 months ago 60
Swift Question

UISwitch - UITableView

I have this tableView:


but I can't recognize when user selects the UISwitch in the row.
I know I need an @IBAction in the UITableViewCell class but I didn't found a guide that works for me.
I'd like to print the indexPath of the row when user clicks on the UISwitch.

This is my class for cell:

class TicketsFilterCell: UITableViewCell {

@IBOutlet weak var textFilter: UILabel!
@IBOutlet weak var switchFilter: UISwitch!

class var reuseIdentifier: String? {
get {
return "TicketsFilterCell"

override func awakeFromNib() {

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)



How can I do?

Thanks in advance.


My cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

let cell: TicketsFilterCell? = tableView.dequeueReusableCellWithIdentifier("cellTicketFilter", forIndexPath: indexPath) as? TicketsFilterCell

cell?.delegate = self

if(indexPath.section == 0) {

let text = status[indexPath.row]?.capitalizedString

cell?.textFilter.text = text

} else if(indexPath.section == 1) {

let text = queues [indexPath.row]?.capitalizedString

cell?.textFilter.text = text

} else if(indexPath.section == 2) {

let text = types[indexPath.row]?.capitalizedString

cell?.textFilter.text = text

} else if(indexPath.section == 3) {

let text = severities[indexPath.row]?.capitalizedString

cell?.textFilter.text = text


return cell!




Here is what you can do :)

Declare a protocol in your cell class lets call it as CellProtocol.

protocol CellProtocol : class {
    func switchButtonTapped(WithStatus status : Bool, ForCell myCell : TicketsFilterCell)

Declare a variable in your TicketsFilterCell class to hold delegate,

weak var delegate : CellProtocol!

When user taps on your switch trigger the delegate as

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    self.delegate .switchButtonTapped(WithStatus: selected, ForCell: self)

In your TableViewController confirm to protocol using

class ViewController: UITableViewController,CellProtocol {

Now in your ViewController's cellForRoAtIndexPath, for each cell set the delegate as self.

cell.delegate = self

Finally implement the delegate method as follow

func switchButtonTapped(WithStatus status: Bool, ForCell cell: TicketsFilterCell) {
        let indexPath = self.tableView .indexPathForCell(cell)
        print("cell at indexpath \(indexPath) tapped with switch status \(status)")


For each cell you have dragged the IBOutlet for switch :) Now all you need is IBAction from UISwitch to the cell :)

@IBAction func switchTapped(sender: UISwitch) {
        self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self)

Thats it :)