Ios 当另一行点击时,如何取消选择该行?
以下是我想要归档的内容:当第二个中文行点击时取消选择“English”行,或者相反 我只想显示一个复选标记,而不是全部。这是我的密码:Ios 当另一行点击时,如何取消选择该行?,ios,swift,uitableview,Ios,Swift,Uitableview,以下是我想要归档的内容:当第二个中文行点击时取消选择“English”行,或者相反 我只想显示一个复选标记,而不是全部。这是我的密码: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView.cellForRow(at: indexPath)!.accessoryType == .checkmark { tableView.cellForR
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)!.accessoryType == .checkmark {
tableView.cellForRow(at: indexPath)!.accessoryType = .none
navigationItem.rightBarButtonItem!.isEnabled = false
} else if tableView.cellForRow(at: indexPath)!.accessoryType == .none {
tableView.cellForRow(at: indexPath)!.accessoryType = .checkmark
navigationItem.rightBarButtonItem!.isEnabled = true
}
}
最后,我得到了一个答案。给你:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)!.accessoryType = .checkmark
navigationItem.rightBarButtonItem?.isEnabled = true
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)!.accessoryType = .none
navigationItem.rightBarButtonItem?.isEnabled = false
}
您可以考虑使用一个变量来保存选定的行索引。并检查cellForRowAtIndexPath…上的selectedRowIndex变量是否为indexPath.row。。。。在didselectRow上,您可以设置selectedRowIndex的值并重新加载tableview。如果您只想选择一个项目,可以设置tableview.allowsMultipleSelection=False。没有冗余(不必要)字且格式正确的代码有什么问题?@LeoDabus它不起作用,如果已删除UITableViewCellAccessoryE。您不会选择任何一行。@LeoDabus谢谢,强制展开cellForRow方法效果很好。@LeoDabus很酷。非常感谢,利奥。