2017-05-01 5 views
0

このコードは、タスクをリストするtableViewController用です。 UIButtonをタップすると、ボタンのタイトルを空の文字列からチェックマークに切り替えることになっています。何らかの理由で、シミュレータのボタンをタップすると何も起こらず、コンソールにエラーが表示されません。誰もそれがトグルしていない理由を知っていますか?参照コードは以下のとおりです。どんな助けでも大歓迎です!みんなありがとう!ここではUIButtonのコードだチェックボックスタップしたときにUIButtonのタイトルが切り替わらない

import UIKit 

class LoLFirstTableViewController: UITableViewController { 

    var tasks:[Task] = taskData 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.rowHeight = UITableViewAutomaticDimension 
     tableView.estimatedRowHeight = 60.0 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return tasks.count 
    } 

    @IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) { 
    } 

    @IBAction func saveAddTask(_ segue:UIStoryboardSegue) { 
     if let AddTaskTableViewController = segue.source as? AddTaskTableViewController { 

      if let task = AddTaskTableViewController.task { 
       tasks.append(task) 

       let indexPath = IndexPath(row: tasks.count-1, section: 0) 
       tableView.insertRows(at: [indexPath], with: .automatic) 
      } 
     } 
    } 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell 

    let task = tasks[indexPath.row] 
     cell.task = task 

     var rowChecked: [Bool] = Array(repeating: false, count: tasks.count) 

    if cell.accessoryView == nil { 
       let cb = CheckButton() 
       cb.addTarget(self, action: #selector(buttonTapped(_:forEvent:)), for: .touchUpInside) 
       cell.accessoryView = cb 
    } 
      let cb = cell.accessoryView as! CheckButton 
      cb.check(rowChecked[indexPath.row]) 

      return cell 
    } 

func buttonTapped(_ target:UIButton, forEvent event: UIEvent) { 
      guard let touch = event.allTouches?.first else { return } 
      let point = touch.location(in: self.tableView) 
      let indexPath = self.tableView.indexPathForRow(at: point) 
     var tappedItem = tasks[indexPath!.row] as Task 
     tappedItem.completed = !tappedItem.completed 
     tasks[indexPath!.row] = tappedItem 
      tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.none) 
    } 

は、ここでのUITableViewControllerコードです

import UIKit 

class CheckButton : UIButton { 
    convenience init() { 
     self.init(frame:CGRect.init(x: 0, y: 0, width: 20, height: 20)) 
     self.layer.borderWidth = 2 
     self.layer.cornerRadius = 10 
     self.titleLabel?.font = UIFont(name:"Georgia", size:10) 
     self.setTitleColor(.black, for: .normal) 
     self.check(false) 
    } 
    func check(_ yn:Bool) { 
     self.setTitle(yn ? "✔" : "", for: .normal) 
    } 
    override init(frame:CGRect) { 
     super.init(frame:frame) 
    } 
    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 
+0

デバッグを試しましたか?例えば。ブレークポイントを 'check'関数にセットするか、そこにログを追加してください。方法は呼ばれていますか?もしそうなら、 'yn'の値をチェック(プリント)してください。 – Nef10

答えて

1

あなただけのすべての呼び出しcheckcb.check(rowChecked[indexPath.row])とし、rowCheckedは常に[false, false, false, ...]の配列です。

これはおそらくbuttonTappedで行っていることに基づいてcb.check(tasks[indexPath.row].completed)になるはずです。

+0

ああ、多分、indexPathの各項目のステータスについてrowCheckedを更新するべきでしょうか? –

+0

rowCheckedは関数内にしか存在しないので、 'tasks'がすでに真実の記録であるので、それを保持する理由はおそらくありません。 –

+0

ああ、良い考え! –

関連する問題