2017-08-24 63 views
1

我的tableview看起來像這樣UISwitch是在其他細胞改變狀態時滾動

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell 
    return cell 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 200 
} 

而且我tableviewcell看起來像這樣

class Cell: UITableViewCell { 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

當我在任何細胞和滾動改變開關狀態。另一個單元的開關週期性地在其他單元的滾動狀態中改變。請幫助

layout of uiswitch

+0

這是因爲電池出隊的。你能否用數據模型更新你的問題? – Baig

+0

這就是問題所在。我創建了測試項目來查看我的數據模型是否會導致此問題。但只有這些代碼,它給我同樣的問題 – cole

+0

請檢查我的更新【答案】(https://stackoverflow.com/a/45857034/1556386) – Baig

回答

4

你的那個狀態需要有狀態數據陣列的每個開關和從陣列在GET開關狀態開關然後在你的cellForRow方法didSelect變化的數據模型和你的viewController必須SwitchControlDelegate

class UIViewController:SwitchControlDelegate{ 

    var array = [Bool](count: 200, repeatedValue:false) 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell 
    cell.switchControl.isOn = array[indexPath.row] 
    cell.switchDelegate = self 
    cell.index = indexPath.row 
    return cell 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 200 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    array[indexPath.row] = !array[indexPath.row] 
} 

func switchChangeStateWithIndex(index:Int){ 
    array[index] = !array[index] 
} 

這將改變開關狀態,當您單擊單元格,如果您需要更改狀態時,轉換開關的控制,你將需要委託更新的viewController

數據模型在你的文件:

protocol SwitchControlDelegate: class { 
     func switchChangeStateWithIndex(index:Int) 
    } 

    class Cell: UITableViewCell { 

     var index:Int = 0 
     weak var switchDelegate: SwitchControlDelegate? 
     override func awakeFromNib() { 
      super.awakeFromNib() 
      // Initialization code 
     } 

     override func setSelected(_ selected: Bool, animated: Bool) { 
      super.setSelected(selected, animated: animated) 

      // Configure the view for the selected state 
     } 

     @IBAction func valueChanged(_ sender: AnyObject) { 
      switchDelegate?.switchChangeStateWithIndex(index:index) 
     } 

} 

您需要以匹配使用此功能的valueChanged您的開關控制動作值的變化,所以當你改變開關狀態的細胞需要調用FUNC值Changeed

+0

嗨Svetoslav,我有你的代碼相同的問題。當我滾動時,所選行的開關發生變化。回到未選中的狀態。 – cole

+0

請參閱我更新的代碼,如果你仍然有問題,然後貼上我的viewController你和cellFile所有IBOutlets和IBActions –

+0

感謝新的代碼,如果它不工作將發佈。但你能告訴cell.delegate嗎?它顯示一個單元格沒有委託方法的錯誤。請幫助解決此問題,以測試您的代碼 – cole

0

妥善處理您的切換控制,以在每個單元,

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 

,這樣,當細胞是出列,它處理每個單元的switchControl正常。

解決方案: 當您更改單元格開關控制的狀態下,保存indexPath的地方,這樣,當您通過本indexPath在tableview中cellForRowAt indexPath重複設置開關再次,否則設置默認狀態

0

這可能發生,因爲細胞是可重複使用的每一次。在你的細胞實施prepareForReuse方法:

class TableCell: UITableViewCell { 
    .... 

    override func prepareForReuse() { 
     super.prepareForReuse() 
     self.yourSwitch.isOn = false 
    } 
} 

是的,你還可以使用類似與各國/對象的數組中的單元格,設置正確的開關值。

+0

我想說,也許最好的方法是不重寫'prepareForReuse',保持模型狀態是更好的方法。 – Stefan

+0

嗨,我試過,但沒有工作。 – cole