Search code examples
iosuitableviewswiftxcode6ibaction

Respond to a UITableCell action in the UIViewController


So I have a UITableViewCell:

enter image description here

Here is the code:

import Foundation
import UIKit

class Cell_Switch: UITableViewCell{

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var select: UISwitch!
    override func awakeFromNib() {
        super.awakeFromNib()
    }

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

    override var layoutMargins: UIEdgeInsets {
        get { return UIEdgeInsetsZero }
        set(newVal) {}
    }
    @IBAction func onSwitch(sender: AnyObject) {
        //I don't want to respond here!!!
    }
}

And here's the UITableView code:

import Foundation
import UIKit

class PreferencesInterestedDays: UIViewController,UITableViewDataSource,UITableViewDelegate{
    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var nib = UINib(nibName: "Cell_Switch", bundle: nil)
        table.registerNib(nib, forCellReuseIdentifier: "Cell_Switch")
        table.separatorInset = UIEdgeInsetsZero

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }





    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:Cell_Switch = self.table.dequeueReusableCellWithIdentifier("Cell_Switch") as Cell_Switch

        //what do I do here???
        //not working: cell.select.actionsForTarget(self, forControlEvent: UIControlEvents.ValueChanged)



        return cell
    }
    //this event doesn't fire!
    @IBAction func onSwitch(sender: AnyObject) {
        //doesn't work... ;(
        println("Hello!")
    }



    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        table.deselectRowAtIndexPath(indexPath, animated: false)
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44.0
    }
}

Here's what it looks like:

enter image description here

Each switch selected has a different response. I need to handle the actions (and get the row number) in the UITableView class, not in the UITableViewCell class...

What to do?

XCode 6.2, iOS 8.2


Solution

  • Simple solution would be

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell:Cell_Switch = self.table.dequeueReusableCellWithIdentifier("Cell_Switch") as Cell_Switch
    
            cell.select.addTarget(self, action: Selector("onSwitch:"), forControlEvents: UIControlEvents.ValueChanged)
    
            return cell
        }
    
        @IBAction func onSwitch(sender: AnyObject) {
            println("Hello!")
        }
    

    Note, remove any reference of IBAction inside Cell_Switch.

    Cheers.