2016-05-29 68 views
0

當我加載表格視圖與選中的單元格,我想取消選中一個特定的單元格我需要點擊兩次單元格取消它,我想我知道問題在哪裏,但我不我該如何解決這個問題?點擊需要兩次來取消表單元格

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 

     } 

    } 

回答

2

只實施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) 
+0

這個解決我的問題,但它只是創造了一個新的,現在當我要檢查或取消選中單元格我需要點擊兩次 – User

+0

如果你打算爲對勾標記,則可能應該取消的細胞在該方法的結束(所以你得到漂亮的高光動畫) - 我已經用該代碼更新了我的答案。 – sschale

+0

你真棒:) – User

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] 

} 
} 
相關問題