2014-10-30 71 views
1

我有一个UITableViewController和一个自定义TableViewCell并在该UITableViewCell内有一个UISwitch。该交换机连接到一个IBAction为,但只要我轻按开关,我得到一个错误:自定义UITableViewCell中的UISwitch IBAction导致错误

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"); 
    } 

} 
+0

您的代码似乎正确。我怀疑这可能是IBOutlet和IBAction之间的接线问题。你可以用一个简单的'println(“test”)'语句替换'delegate.selectUser(“test”)'来测试吗? – anthonyliao 2014-10-31 02:52:46

+0

我用一个println替换它,并且给了我完全相同的错误。但是,正如我所看到的那样,IBAction似乎已连接到交换机。也就是说,如果我指向IBAction前面的点,故事板上的开关就会点亮。 – Tys 2014-10-31 07:59:47

+0

我更新了我的问题,以便现在解决确切的问题。 – Tys 2014-10-31 10:56:00

回答

2

你有这个代码一些奇怪的东西:

  1. 你的cellForRowAtIndexPath应返回单元格(您可能已经这样做了,只是没碰到过将它复制到你的计算器问题)

  2. 您正在通过故事板或xib生成您的UISwitch,正如您所说'故事板上的开关突出显示' - 但是您也正在代码中实例化这些内容! 例如。

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

但我相信最终你的问题与你的IBAction为 'SwitchUser'。您在某个时候重新命名了此方法,或者先创建了一个IBAction,然后将其删除。要检查IBActions的当前状态,请单击故事板或xib中的单元格,然后打开Connections Inspector。我敢打赌你会在那里发现你的问题。

+0

是的,我在Connections Inspector中发现了我的问题!我不知道那件事存在。谢啦!确实有3个功能相连,从中显然只有1个功能依然存在。我想这确实来自重命名该功能。 – Tys 2014-10-31 23:25:50