2016-05-29 14 views
0

チェックしたセルでテーブルビューを読み込み、特定のセルのチェックを外したい場合は、セルを2回タップしてチェックを外す必要がありますが、どのように私はこの問題を解決することができますか?テーブルセルのチェックを外すには2回タップする必要があります

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を実装する必要があります。そこでは、ちょうどあなたのdidDeselectRowAtIndexPathブロックを削除し、あなたの次のコードでdidSelectRowAtIndexPathブロックを交換し、それが働いているなら、私に教えて、選択状態を反転

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

を行います。

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] 

} 
}