TableView didSelectRowAt IndexPath未被调用

时间:2017-08-24 17:55:32

标签: ios swift uitableview

我当前正在设置一个分组表视图,当视图加载时,根据从用户默认值中提取的结果,在一个区域中的tableview单元格上设置复选框。当用户点击该部分中的另一个单元格时,另一个单元格上的复选框将消失,并且他们刚刚点击的单元格上会显示一个新的复选框。

这似乎在最初点击不同的单元格时有效,但在3次更改后,您必须点击单元格两次才能更改复选框。看起来每个其他的tap 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
}

1 个答案:

答案 0 :(得分:1)

取消选择动作执行到didSelectRowAt

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

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

}