2016-10-01 101 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之前,整个情况都很顺利。 –