Tys
Tys

Reputation: 3610

IBAction on UISwitch in custom UITableViewCell causes error

I have a UITableViewController and a custom TableViewCell and within that UITableViewCell there is a UISwitch. This switch is wired to an IBAction, but as soon as i tap the switch, i get an error:

 unrecognised selector sent to instance 0x13ce30a50

SelectFriendsViewController.swift

class SelectFriendsViewController: UITableViewController, SelectorDelegate {
    func selectUser(string: String) {
        selectedUser = string;
    }

    .... lots of code removed for simplification.

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

        let cell:SelectorTableViewCell = tableView!.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as SelectorTableViewCell;

        cell.delegate = self;
    }

}

protocol SelectorDelegate {
    func selectUser(string: String)
}

class SelectorTableViewCell: UITableViewCell {

    @IBOutlet var swUser: UISwitch! = UISwitch();
    @IBOutlet var lblUserName: UILabel! = UILabel();

    var delegate: SelectorDelegate!

    @IBAction func SwitchUser(sender: UISwitch) {
        //delegate.selectUser("test");
        //even with just this println i get the error
        println("test");
    }

}

Upvotes: 1

Views: 557

Answers (2)

Craig Grummitt
Craig Grummitt

Reputation: 2995

You have some strange stuff in this code:

  1. your cellForRowAtIndexPath should return a cell (you probably have done this, and just didn't copy it across to your stackoverflow question)

  2. You're generating your UISwitch either from storyboard or xib as you say 'the switch on the storyboard is highlighted' - however you're also instantiating these in code! eg.

    @IBOutlet var swUser: UISwitch! = UISwitch();
    

But I believe your problem in the end is related to your IBAction 'SwitchUser'. You either renamed this method at some point or created an IBAction earlier and then deleted it. To check the current status of your IBActions, click on your cell in the storyboard or xib and open the Connections Inspector. I bet you'll find your problem there.

Upvotes: 2

Related Questions