2016-10-01 13 views
2

私はSwift 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") 
} 
  • 代理人はあなたのTaskCellインスタンスに正しくdelegateプロパティを設定しなかったように、私は非常に基本的な例を行いますルックスうまくいけば、それはあなたを助けます問題をキャッチする:

    結果(編集済み)

    コード

    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(編集後)

    代わりにあなたがaddTargetメソッドを使用することができ、チェックボックスに接続する新しいUITapGestureRecognizerを作成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. View Controllerがプロトコルに準拠していません。

      class ViewController: UIViewController, TaskCellDelegate { 
      
    3. プロトコルメソッドがView Controller内で実装されていません。 cellForRowAtIndexPathMethod:

      cell.delegate = self 
      
    +0

    編集内容をご覧ください。私はこれらすべてのことをやった。この全体的な状況は、私が移行を3にする前にうまくいきました。 –

    関連する問題