2014-09-22 25 views
0

我在我的自定義UITableViewCell中插入了UISwitch元素,並且我的UITable中有100多個單元。ios swift - 爲什麼我點擊UISwitch打開,其他UISwitchs同時打開?

它顯示多個UISwitch,我可以在UITable中打開和關閉哪個UISwitch。

但我發現了一些奇怪的東西。

當我點擊第一個UISwitch時,第13,26,38,51 .... UISwitch同時改變其狀態。

不管他們是哪一部分,我改變一個UISwitch狀態,並且另一個UISwitch改變。

那些改變的UISwitch的間隔是11,12,11,12,11,12 ......元素。

我點擊第13個UISwitch OFF,第1個,第26個,第38個,...... UISwitch變爲OFF。

我試圖應用分段控制而不是UISwitch,並且我得到了這些元素的相同動作。

我把這些代碼寫在我的ViewController中。

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

     let cell : CardListCell = tableView.dequeueReusableCellWithIdentifier("RandomCardCell") as CardListCell 
     var person = cardArray1[indexPath.item] 
     cell.setCell(person.cardName, leaderName: person.leaderName, cardNo: person.cardNo, groupName: "") 
     cell.countDelegate = self 
     //this is my delegate claimed in the Cell, and implemented in the View Controller 

     return cell 
} 

在我的單元格中,我創建了一個函數來設置自定義單元格的值。

func setCell(cardName: String, leaderName: String, cardNo: Int, groupName: String) 
{ 
    self.cardNameLabel.text = cardName 
    self.leaderNameLabel.text = leaderName 
    self.cardNo = cardNo 
    self.groupName = groupName 
} 

我現在沒有在UISwitch操作中做任何事情。

@IBAction func switchAction(sender: AnyObject) { 

} 

當我設置自定義單元格時,是否會錯過任何東西?

我只想輕點一下,只有一個UISwitch發生變化,沒有多個變化。

回答

0

在從cellForRowAtIndexPath返回之前,您需要將開關設置爲開啓或關閉。如果只打開它,然後使用由tableView.dequeueReusableCellWithIdentifier返回的單元格,則在重新使用舊單元格時將獲得緩存值。

+0

這意味着我必須檢查抽頭結果並設置所有開關狀態,然後才能返回包含開關元素的任何單元。對?至少,我通過這種方式解決了這個問題。 – 2014-09-23 08:32:06

+0

是的。通常情況下,你會給'person'一個'Bool'屬性,如「selected」,並在點擊時設置爲true或false,然後在返回單元格之前使用它。 – 2014-09-23 12:15:01

+0

非常感謝。^___ ^ – 2014-09-24 09:27:12