【问题标题】:TableView didSelectRowAt IndexPath not getting calledTableView didSelectRowAt IndexPath 没有被调用
【发布时间】:2017-08-24 17:55:32
【问题描述】:

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

这似乎在最初点击不同的单元格时有效,但在 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
}

【问题讨论】:

  • 乍一看,在我看来,您正在尝试通过检查单元格索引路径来确定复选框的状态;这可能是导致它中断的原因,因为单元格异步排队和出队。尝试将选中状态存储在数据源中,并在单元格出现时更新复选标记?
  • 可能不相关,但永远不会自己调用包含didwillshould的委托方法。有一个方法deselectRow(at:animated:
  • 感谢您的及时回复!我对 iOS 很陌生,我不确定你在数据源中存储检查状态是什么意思
  • 啊,是的!删除 tableView(didSelectRowAt) 中的委托方法并处理 deselectRow(at:animated:) 中的取消选择通过一些调整解决了这个问题,非常感谢!

标签: ios swift uitableview


【解决方案1】:

取消选择操作执行到 didSelectRowAt

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多