2014-10-30 69 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