2017-01-02 69 views
-3

我已经通过few of these tutorial,我似乎无法通过按钮点击调用我的功能。我跟着这个人的tutorial,但没有任何工作。行动按钮不工作在UITable

tableView目前正在工作很好,所有我现在正在做的是添加一个按钮,所以我可以传递数据到另一个视图,但我的按钮单击不调用它的功能。

//class IncomeFeedCell: UITableViewCell: 

@IBOutlet var weak button: UIButton! 

查看位指示:

// 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,因为我离开了更多的重要信息。所有这一切都在我的viewDidLoadIncomeFeedVC

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)this line and func buttonPressed(){ print(“Tapped”) } funcation –

+0

您的问题不清楚。确切的问题是什么?当你点击按钮会发生什么? – rmaddy

回答

-4

您的按钮窃听功能应该是这样的:

func buttonPressed(sender: UIButton) 
    { 

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

    } 
+0

这是如何解决他的问题? –

+3

不需要'sender'参数。添加这个并不能回答这个问题。 – rmaddy

+1

有人说'sender'参数不需要,是的,你不需要为事件处理程序提供任何参数,但这是一种糟糕的编程行为。即使您不使用它,您也应该始终将'sender'传递给UIControl的任何事件处理程序。 – 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(sender: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行中的“self”。

+1

这是如何解释问题中的问题的?为什么OP代码中没有调用'buttonPressed'方法? – rmaddy

+0

@maddy我只是添加了一个工作参考。和OP的代码不能正常工作的cellForRow函数我也建议他的代码中可能是错的。 – theduman

+0

几乎所有的答案中的代码都与这个问题无关,除了一个func。 –