2017-05-01 42 views
0

此代碼適用於列出任務的tableViewController。當輕按UIButton時,它應該將按鈕的標題從空字符串切換到複選標記。出於某種原因,當我點擊模擬器中的按鈕時,沒有任何反應,控制檯中也沒有顯示錯誤。任何人都知道它爲什麼不切換?參考代碼如下。任何幫助將不勝感激!謝謝大家!複選框UIButton標題在切換時不會翻轉

這裏的代碼的UITableViewController:

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) 
    } 

下面是對的UIButton代碼:

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

你試過調試過嗎?例如。在「檢查」功能中設置一個斷點或在那裏添加一個日誌。該方法甚至被稱爲?如果是這樣,請檢查(打印)'yn'的值 – Nef10

回答

1

你只在每次調用checkcb.check(rowChecked[indexPath.row])rowChecked始終是[false, false, false, ...]陣列。

這應該可能是cb.check(tasks[indexPath.row].completed)根據你在buttonTapped做的事情。

+0

嗯,也許我應該每次都檢查indexPath中每個項目的狀態。 –

+0

由於rowChecked只存在於函數中,因此可能沒有理由將它保留爲'tasks'已經是真值的記錄。 –

+0

啊,好想法! –