2016-10-01 99 views
2

我以前的工作委託和協議的是,由於轉換到夫特3不再被調用。委託功能沒有被調用

protocol TaskCellDelegate { 
    func doneHit(_ cell : TaskCell) 
} 

class TaskCell : UITableViewCell { 

    var delegate : TaskCellDelegate? 

    @IBOutlet weak var label: UILabel! 
    @IBOutlet weak var detailLabel: UILabel! 
    @IBOutlet weak var _checkBox: M13Checkbox! 


    override func awakeFromNib() { 
     super.awakeFromNib() 
     let tap = UITapGestureRecognizer(target: self, action: #selector(TaskCell.buttonClicked(_:))) 
     tap.numberOfTapsRequired = 1 
     _checkBox.addGestureRecognizer(tap) 
     _checkBox.isUserInteractionEnabled = true 
     _checkBox.markType = .checkmark 
     _checkBox.boxType = .circle 
     _checkBox.stateChangeAnimation = .expand(.fill) 
    } 
    func buttonClicked(_ sender:UITapGestureRecognizer) { 
     delegate?.doneHit(self) 
    } 
} 

正如你可以看到,當_checkBox被竊聽應該調用函數doneHit在我的班級(不加,因爲它似乎沒有必要的,但我可以),但我設置一個斷點,它永遠不會被調用。我已經設置了我的委託並符合我班的協議,但沒有任何事情發生。 doneHit函數應該更新我的後端,但它沒有被調用。如果您需要更多信息,我可以提供。

編輯1:

class TasksTVC: UITableViewController, TaskCellDelegate { 

    func doneHit(_ cell:TaskCell) { 
     if let indexPath = self.tableView.indexPath(for: cell) { 
      task = tasksInSectionArray[indexPath.section][indexPath.row] 
      if task.done == false { 
       cell._checkBox.setCheckState(.checked, animated: true) 
       task.done = true 
       task.completedBy = user 
       cell.detailLabel.text = "Completed By: \(task.completedBy)" 
       cell.label.textColor = UIColor.gray 
       print("cell checked") 
      } 
      else { 
       cell._checkBox.setCheckState(.unchecked, animated: true) 
       task.done = false 
       task.completedBy = "" 
       cell.detailLabel.text = "" 
       cell.label.textColor = UIColor.black 
       print("cell unchecked") 

      } 
      fb.updateTaskDoneBool(ref, taskID: task.id, taskDone: task.done) 
      fb.updateTaskCompletedBy(ref, taskID: task.id, taskCompletedBy: task.completedBy) 
     } 

    } 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell 
     cell.selectionStyle = .none 
     task = tasksInSectionArray[indexPath.section][indexPath.row] 
     cell.label.text = task.title 
     if task.done == true { 
      cell._checkBox.setCheckState(.checked, animated: true) 
      cell.detailLabel.text = "Completed By: \(task.completedBy)" 
      cell.label.textColor = UIColor.gray 
     } 
     else { 
      cell._checkBox.setCheckState(.unchecked, animated: true) 
      cell.detailLabel.text = "" 
      cell.label.textColor = UIColor.black 

     } 
     doneHit(cell) 
     cell.delegate = self 
     return cell 
    }} 
+1

'buttonClicked'被稱爲?顯示你的'cellForRowAtindexPath'方法。 –

回答

1

未分配

func doneHit(_ cell : TaskCell) { 
    print("delegate implementation called") 
} 
  • 委託看起來你沒有正確的delegate屬性設置在TaskCell情況下,我會做一個非常簡單的例子希望它可以幫助你以解決問題:

    結果(已編輯)

    代碼

    TableViewController

    import UIKit 
    
    protocol TaskCellDelegate { 
        func doneHit(_ cell: TaskCell) 
    } 
    
    class TableViewController: UITableViewController, TaskCellDelegate { 
        func doneHit(_ cell: TaskCell) { 
        let alert = UIAlertController(
            title: "Info", 
            message: "button touched in cell", 
            preferredStyle: .alert 
           ) 
        present(alert, animated: true, completion: nil) 
        } 
    } 
    
    extension TableViewController { 
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
        return 1 
        } 
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TaskCell 
        cell.delegate = self // probably you forgot to set this part? 
    
        return cell 
        } 
    } 
    

    TaskCell(編輯)

    不是創建一個新UITapGestureRecognizer附加到複選框,您可以使用addTarget方法爲UIControlEvents.valueChanged值附加事件處理程序。

    import UIKit 
    import M13Checkbox 
    
    class TaskCell: UITableViewCell { 
    
        var delegate: TaskCellDelegate? 
    
        @IBOutlet weak var checkbox: M13Checkbox! 
    
        override func awakeFromNib() { 
        super.awakeFromNib() 
    
        checkbox.addTarget(self, action: #selector(buttonClicked), for: .valueChanged) 
        } 
    
    
        func buttonClicked() { 
        delegate?.doneHit(self) 
        } 
    
    } 
    
  • +0

    這並沒有考慮到我將buttonClicked作爲我的輕擊手勢識別器上的動作。這意味着我有一個在IB中創建的按鈕,並使用它來觸發該功能。 –

    +0

    @MichaelWilliams我已經更新了我的答案。 – Wilson

    0

    有下列情況下,如果委託沒有被稱爲:

    1. 動作:buttonClicked不會被調用。
    2. 視圖控制器不符合協議。

      class ViewController: UIViewController, TaskCellDelegate { 
      
    3. 該協議方法沒有在View Controller中實現。在cellForRowAtIndexPathMethod:

      cell.delegate = self 
      
    +0

    請參閱修改。我已經完成了所有這些事情。在我完成遷移到swift 3之前,整個情況都很順利。 –