当我加载带有选中单元格的表格视图并且我想取消选中特定单元格时,我需要在单元格上点击两次以取消选中它,我想我知道问题出在哪里,但我不知道如何解决这个问题?
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
}
}
Run Code Online (Sandbox Code Playgroud)
你应该只实现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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
687 次 |
| 最近记录: |