2017-08-24 50 views
1

我目前正在設置一個分組表格視圖,當視圖加載取決於從用戶默認值拉取的結果時,在一個區域的tableview單元格中設置了複選框。當用戶點擊該部分中的不同單元格時,其他單元格上的複選框將消失,並且將在他們剛剛點擊的單元格上顯示新的複選框。TableView didSelectRowAt IndexPath沒有被調用

這似乎工作時,最初輕敲不同的單元格,但3次更改後,你必須點擊單元格兩次複選框才能更改。它看起來像其他每個點擊tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)沒有被調用。

感謝提前任何幫助,下面的代碼:

var selectedIndex: IndexPath? = nil 

let usersettings = UserDefaults.standard 

override func viewDidLoad() { 
    super.viewDidLoad() 
    settingsTableView.delegate = self 
    settingsTableView.dataSource = self 
    //Load current preferences from user defaults 
    spamNumbersSetting = usersettings.value(forKey: "spamNumbersSetting") as! Int 
    personalNumbersSetting = usersettings.value(forKey: "personalNumbersSetting") as! Int 
    settingsSaveButton.isEnabled = false 
    // Do any additional setup after loading the view. 
} 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){ 
    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.textColor = UIColor.white 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return settingsHeaders.count 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return settingsHeaders[section] 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return settingsContent[section].count 
} 

func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell { 

    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) 
    //Preset ticks depending on value of user settings 
    if indexPath.section == 0 { 
     if (spamNumbersSetting == 1) { 
      if indexPath.row == 0 { 
       cell.accessoryType = UITableViewCellAccessoryType.checkmark 
       selectedIndex = indexPath 
      } 
     } else { 
      if indexPath.row == 1 { 
       cell.accessoryType = UITableViewCellAccessoryType.checkmark 
       selectedIndex = indexPath 
      } 
     } 
    } 

    if indexPath.section == 1 { 
     if (personalNumbersSetting == 1){ 
      cell.accessoryType = UITableViewCellAccessoryType.checkmark 
     } 
    } 

    cell.textLabel?.textColor = UIColor.white 
    let section = indexPath.section 
    cell.textLabel?.text = settingsContent[section][indexPath.row] 


    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    self.tableView(tableView, didDeselectRowAt: selectedIndex!) 
    settingsSaveButton.isEnabled = true 
    let newCell = tableView.cellForRow(at: indexPath) 
    print("\(newCell?.textLabel?.text) is now the active cell !") 
    let oldCell = tableView.cellForRow(at: selectedIndex!) 
    //Deselect old cell 
    if indexPath.section == 0 { 
     if newCell?.accessoryType != .checkmark { 
      newCell?.accessoryType = .checkmark 
      //oldCell?.accessoryType = .none 
     } else { 
      newCell?.accessoryType = .none 
     } 

     selectedIndex = indexPath 
    } 

} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 
    print("Deselected \(cell?.textLabel?.text) !") 
    cell?.accessoryType = .none 
} 
+0

乍一看,它看起來像你對我正在試圖確定通過檢查單元索引路徑來檢查複選框的狀態;這可能是因爲細胞異步排隊和出隊造成的。嘗試將選中狀態存儲在數據源中,並在單元格出現時更新複選標記? – brandonscript

+0

也許不相關,但**永遠不會**調用你自己的包含'did','''''''''''應該''的委託方法。有一種方法'deselectRow(at:animated:' – vadian

+0

感謝您的快速響應!我對iOS很新,我不確定你的意思是將選中的狀態存儲在數據源中 – DannyBoi8181

回答

1

取消行動進行到didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     tableView .deselectRow(at: indexPath, animated: false) 

} 
相關問題