2017-01-02 5 views
-3

私はfew of these tutorialを経由しています。ボタンをクリックすることで私の機能を呼び出すようです。私はこの男のtutorialに従っているが、何も動かない。アクションボタンがUITable内で動作しない

私のtableView現在のところうまく働いていますが、私は今、データを別のビューに渡すことができるようにボタンを追加していますが、ボタンのクリックでその機能が呼び出されていません。

//class IncomeFeedCell: UITableViewCell: 

@IBOutlet var weak button: UIButton! 

ビューContoller:今

// class IncomeFeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource: 

// viewDidLoad: 
tableView.delegate = self 
tableView.dataSource = self 

// cellForRowAt indexPath 
let cell = self.tableView.dequeueReusableCell(withIdentifier: "IncomeFeedCell") as! IncomeFeedCell 
cell.button.tag = indexPath.row 
cell.button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) 
cell.configureCell(income: income) 
return cell 

タップしたときに呼び出す関数:

func buttonPressed(){ 
    print("Tapped") 
} 

シミュレータ:

enter image description here

私は何かを逃したことがありますか?

編集:と助けようとしたと私はより多くの重要な情報を残しているためdownvotedてしまったすべての人のための

謝罪。このすべてがIncomeFeedVCに私のviewDidLoad内にある:

super.viewDidLoad() 
tableView.delegate = self 
tableView.dataSource = self 

DataService.ds.REF_INCOMES.queryOrdered(byChild: "date").observe(.value, with: { (snapshot) in 

    /** 
    * Sorting the object before looping over 
    * Info here: http://stackoverflow.com/questions/41416359/sort-firebase-data-with-queryordered-by-date 
    */ 
    guard let incomeSnap = snapshot.value as? [String:AnyObject] else{ 

     return 
    } 

    let sorted = incomeSnap.sorted{($0.0.value["date"] as! Double) > ($0.1.value["date"] as! Double)} 

    for snap in sorted { 
     if let incomeDict = snap.value as? [String: AnyObject] { 
      let key = snap.key 
      let income = Income(incomeId: key, incomeData: incomeDict) 
      self.incomes.append(income) 
     } 

    } 
    self.tableView.reloadData() 
}) 
+0

@HimanshuMoradiya何がどこにありますか? – Sylar

+0

cell.button.addTarget(self、action:#selector(buttonPressed)for:.touchUpInside)この行とfunc buttonPressed(){ print( "Tapped") }ファンクション –

+0

質問が不明です。正確な問題は何ですか?ボタンをタップするとどうなりますか? – rmaddy

答えて

-4

あなたのボタンタップ機能は次のようにする必要があります:

func buttonPressed(sender: UIButton) 
    { 

    let buttonTag = sender.tag// you can check tag like this 

    } 
+0

これはどのようにして彼の問題を解決しますか? –

+3

'sender'パラメータは必要ありません。これを追加しても問題には答えません。 – rmaddy

+1

誰かが 'sender'パラメータは必要ないと言っています。はい、イベントハンドラにパラメータを指定する必要はありませんが、プログラミングの動作は悪いです。 UIControlのイベントハンドラには必ず 'sender'を渡す必要があります。 – bubuxu

-4

あなたは内に実装する必要があります。また機能の下

cell.button.addTarget(self, action: #selector(IncomeFeedVC.buttonPressed(_:)), for: .touchUpInside) 

を試してみてくださいすることができIncomeFeedVCクラス

func buttonPressed(_ sender: AnyObject){ 
    print("Tapped") 
} 
+0

同じです。何も印刷されていません。私は 'IncomeFeedVC'に関数を持っています。奇妙なもの – Sylar

+0

@sylar IncomeFeedVCクラスでfunc buttonPressed(送信側:UIButton)を実装しましたか? – Manoj

+0

@Manoj彼が言ったように、彼の機能がIncomeFeedVCにあるかどうかは、この形式にすることは重要ではありません。ほとんど私はこれをDVするべきだと思う。 –

-2

私のViewController:私の場合は正常に動作

class ViewController: UIViewController , UITableViewDelegate,UITableViewDataSource{ 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as! TestTableViewCell 

    cell.bttn.addTarget(self, action: #selector(clickme(sender:)), for: .touchUpInside) 
    return cell 
} 
func clickme(sender: UIButton) { 
    print("cliked") 
} 


} 

私のUITableViewCell

import UIKit 

class TestTableViewCell: UITableViewCell { 

@IBOutlet weak var bttn: UIButton! 
override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

その。 let cell = self.tableView.dequeueReusableCell(withIdentifier: "IncomeFeedCell") as! IncomeFeedCell行の「自己」を削除できますか?

+1

これはどのように問題の問題を説明していますか? OPのコードで 'buttonPressed'メソッドが呼び出されないのはなぜですか? – rmaddy

+0

@maddy私はちょうど作業参照を追加しました。そして、OPのコードがおそらくcellForRow関数の働きをしていません。自分のコードで何が間違っているのかも示唆しました。 – theduman

+0

答えのほとんどすべてのコードは、1つの関数とは別に、この質問とは無関係です。 –

関連する問題