Swift 当用户单击按钮时,如何取消选择UITableView单元格?

Swift 当用户单击按钮时,如何取消选择UITableView单元格?,swift,uitableview,Swift,Uitableview,我正在做一个项目,在这个项目中我必须选择一个特定的单元格。我可以在我的UITableView中选择一个单元格,但我想在用户单击Button-RESET-时取消选择所有单元格,因此我不知道如何才能做到这一点。我需要帮助 选择单元格的我的代码: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //changer la couleur de la cellule à la selec

我正在做一个项目,在这个项目中我必须选择一个特定的单元格。我可以在我的
UITableView
中选择一个单元格,但我想在用户单击Button-RESET-时取消选择所有单元格,因此我不知道如何才能做到这一点。我需要帮助

选择单元格的我的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //changer la couleur de la cellule à la selection
    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
    selectedCell.contentView.backgroundColor = UIColor.darkGray

    if !Array( addObject.dictionnary.values)[indexPath.row] {
        addObject.dictionnary[Array( addObject.dictionnary.keys)[indexPath.row]] = true
    } else {
        addObject.dictionnary[Array( addObject.dictionnary.keys)[indexPath.row]] = false
    }
    tableView.reloadData()
}
你可以用

 tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
你可以用

 tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)

下面是一个快速的代码片段,它可能会引导您走向正确的方向

@IBAction func onResetButtonTouchUpInside(sender: AnyObject) {
     if let selectedIndexPaths = tableView.indexPathsForSelectedRows {
          for indexPath in selectedIndexPaths {
                tableView.deselectRow(at: indexPath, animated: true)
          }
     }
}

下面是一个快速的代码片段,它可能会引导您走向正确的方向

@IBAction func onResetButtonTouchUpInside(sender: AnyObject) {
     if let selectedIndexPaths = tableView.indexPathsForSelectedRows {
          for indexPath in selectedIndexPaths {
                tableView.deselectRow(at: indexPath, animated: true)
          }
     }
}

我正在寻找这个,在上找到了一些来自苹果的文档,但它显示了将呼叫放在ViewWillExample。Xcode不喜欢它,所以经过更多的研究,最终发现这是放置它的地方

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

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

我正在寻找这个,在上找到了一些来自苹果的文档,但它显示了将呼叫放在ViewWillExample。Xcode不喜欢它,所以经过更多的研究,最终发现这是放置它的地方

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

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

在这一行中:class AppDelegate:UIResponder,UIApplicationLegate{}崩溃后的堆栈跟踪是什么?您以前是否有在情节提要中连接按钮的其他方法?其中,您删除了按钮所连接的方法?故事板中的插座将用感叹号标记。抱歉,我的按钮已连接到另一个方法,现在我没有错误,但单击我的按钮时我的行未取消选择。我不明白我在哪里加了一个感叹号(对不起,我是swift初学者)。@s.satcha你有我可以提交并向你展示修复程序的源代码吗?你不必在任何地方打感叹号。在故事板中,当元素与iAction或iOutlet之间存在不良连接,而不是具有填充圆时。圆圈上方有一个感叹号。@IBAction func resetButton(u发件人:Any){如果让selectedIndexPaths=tableView.indexPathsForSelectedRows{对于selectedIndexPaths中的indexPath{tableView.DecelesRow(at:indexPath,动画:false)}在这一行中:class AppDelegate:UIResponder,UIApplicationLegate{}崩溃后的堆栈跟踪是什么?您以前是否有在情节提要中连接按钮的其他方法?其中,您删除了按钮所连接的方法?故事板中的插座将用感叹号标记。抱歉,我的按钮已连接到另一个方法,现在我没有错误,但单击我的按钮时我的行未取消选择。我不明白我在哪里加了一个感叹号(对不起,我是swift初学者)。@s.satcha你有我可以提交并向你展示修复程序的源代码吗?你不必在任何地方打感叹号。在故事板中,当元素与iAction或iOutlet之间存在不良连接,而不是具有填充圆时。圆圈上方有一个感叹号。@IBAction func resetButton(u发件人:Any){如果让selectedIndexPaths=tableView.indexPathsForSelectedRows{对于selectedIndexPaths中的indexPath{tableView.DecelesRow(at:indexPath,动画:false)}