Ios 自定义UITableViewCell中UISwitch上的IBAction导致错误

Ios 自定义UITableViewCell中UISwitch上的IBAction导致错误,ios,swift,Ios,Swift,我有一个UITableViewController和一个自定义的TableViewCell,在该UITableViewCell中有一个ui开关。此开关连接到iAction,但只要我点击开关,就会出现错误: unrecognised selector sent to instance 0x13ce30a50 选择Friendsviewcontroller.swift class SelectFriendsViewController: UITableViewController, Select

我有一个
UITableViewController
和一个自定义的
TableViewCell
,在该UITableViewCell中有一个ui开关。此开关连接到iAction,但只要我点击开关,就会出现错误:

 unrecognised selector sent to instance 0x13ce30a50
选择Friendsviewcontroller.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;
    }

}


尝试将函数名更改为switchUser而不是switchUser


这段代码中有一些奇怪的东西:

  • 您的cellForRowAtIndexPath应该返回一个单元格(您可能已经这样做了,只是没有将其复制到stackoverflow问题中)

  • 您正在从storyboard或xib生成UISwitch,正如您所说的“storyboard上的开关高亮显示”-但是您也在代码中实例化它们! 例如


  • 但我相信你的问题最终与你的IBAction“SwitchUser”有关。您可以在某个时候重命名此方法,或者在前面创建了iAction,然后将其删除。要检查iBaction的当前状态,请单击情节提要或xib中的单元格,然后打开连接检查器。我打赌你会在那里发现你的问题。

    你的代码似乎是正确的。我怀疑这可能是IBOutlet和IBAction之间的接线问题。你能用一个简单的
    println(“test”)
    语句替换
    delegate.selectUser(“test”)
    来进行测试吗?我用println替换了它,这就产生了完全相同的错误。但是,正如我所看到的,iAction似乎连接到了交换机。也就是说,如果我指向iAction前面的点,故事板上的开关将亮起。我更新了我的问题,以便它现在能够解决确切的问题。是的,我在连接检查器中找到了我的问题!我不知道那东西存在。谢谢你,伙计!确实有3个功能连接在一起,显然只有1个功能仍然存在。我想这确实是因为重命名了函数。
    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");
        }
    
    }
    
    @IBOutlet var swUser: UISwitch! = UISwitch();