本文介绍了需要点击两次才能取消选中表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我加载带有选中单元格的表格视图并且我想取消选中特定单元格时,我需要在单元格上点击两次以取消选中它,我想我知道问题出在哪里,但我不知道如何解决这个问题?
When I load table view with checked cells and I want to uncheck a specific cell I need to tap twice on cell to uncheck it, I guess I know where's the problem but I don't how I can solve this issue?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("daySelected", forIndexPath: indexPath)
cell.selectionStyle = .None
cell.textLabel?.text = days[indexPath.row]
if indexPath.row == 0 && day[0] == true{
cell.accessoryType = .Checkmark
}
return cell
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .Checkmark
}else{
cell!.accessoryType = .None
}
if indexPath.row == 0{
day[0] = true
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
if indexPath.row == 0{
day[0] = false
}
}
推荐答案
你应该只实现 didSelectRowAtIndexPath
,而不是 didDeselectRowAtIndexPath
.在那里,要翻转选择状态,请执行
You should only implement didSelectRowAtIndexPath
, not didDeselectRowAtIndexPath
. In there, to just flip the selection status, do
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .Checkmark {
cell!.accessoryType = .None
}else{
cell!.accessoryType = . Checkmark
}
}
if indexPath.row == 0{
//flip the day bit
day[0] = !day[0]
}
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
这篇关于需要点击两次才能取消选中表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!