当我用已检查的单元格加载表格视图并且我想取消选中一个特定的单元格时,我需要在单元格上点按两次以取消选中它,我想我知道问题出在哪里但我不知道我是怎么做的可以解决这个问题吗?
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
}
}
答案 0 :(得分:4)
您应该只实施didSelectRowAtIndexPath
,而不是didDeselectRowAtIndexPath
。在那里,只需翻转选择状态,执行
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)
答案 1 :(得分:1)
移除didDeselectRowAtIndexPath
阻止,并使用以下代码替换didSelectRowAtIndexPath
阻止,并告诉我它是否有效。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let cell = tableView.cellForRowAtIndexPath(indexPath)
if indexPath.row == 0
{
if day[0] = true
{
cell!.accessoryType = .None
}
else
{
cell!.accessoryType = .Checkmark
}
day[0] = !day[0]
}
}