【问题标题】:Action button not working within UITable操作按钮在 UITable 中不起作用
【发布时间】:2017-01-02 05:53:01
【问题描述】:

我已经通过few of these tutorial,我似乎无法通过按钮单击调用我的函数。我关注了这个人的tutorial,但没有任何效果。

我的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")
}

模拟器:

我错过了一些简单的事情吗?

编辑:

向所有试图提供帮助但被否决的人道歉,因为我遗漏了更多重要信息。所有这些都在我的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()
})

【问题讨论】:

  • @HimanshuMoradiya 请问这是什么,在哪里?
  • cell.button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) 这一行和 func buttonPressed(){ print("Tapped") } 函数
  • 您的问题不清楚。确切的问题是什么?当您点击按钮时会发生什么?
  • @rmaddy 点击时,控制台不会打印任何内容。
  • @Hrishikesh 如果cell.buttonnil,则尝试使用nil 引用时会发生崩溃。

标签: ios swift uitableview uibutton swift3


【解决方案1】:

我的视图控制器:

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 线。

【讨论】:

  • 这如何解释问题中的问题?为什么在 OP 的代码中没有调用 buttonPressed 方法?
  • @maddy 我刚刚添加了一个工作参考。并且 OP 的代码可能对 cellForRow 函数不起作用我还建议他的代码可能有什么问题。
  • 除了一个函数之外,您答案中的几乎所有代码都与这个问题无关。
  • self 被删除但仍然相同。另外,我正在重新加载viewDidLoad 中的tableView。这会是个问题吗?
  • @Sylar no,你在viewDidLoad中调用tableView.reloadData()没有问题。
【解决方案2】:

您的按钮点击功能应该是这样的:

  func buttonPressed(sender: UIButton)
  {

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

  }

【讨论】:

  • 不需要sender 参数。添加这个并不能回答问题。
  • 有人说不需要sender 参数,是的,您不需要向事件处理程序提供任何参数,但这是一种糟糕的编程行为。即使您不使用它,您也应该始终将 sender 传递给 UIControl 的任何事件处理程序。
  • @sagar 他的 func buttonPressed(sender: UIButton) 方法没有调用。
  • 同意@bubuxu 发件人应该在场是好的编程但不是强制性的
  • 很好,但这并不一定能解决 OP 的问题。
【解决方案3】:

你可以试试

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

下面的函数也应该在 IncomeFeedVC 类中实现

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

【讨论】:

  • 相同。什么都没有打印。我在IncomeFeedVC 有这个功能。奇怪的一个
  • @sylar 你在 IncomeFeedVC 类中实现了 func buttonPressed(sender: UIButton) 吗?
  • @Manoj 如果他的函数像他所说的那样在 IncomeFeedVC 中,那么采用这种格式应该无关紧要。差点觉得我应该 DV 这个。
  • 另外,为什么要AnyObject?它是一个按钮调用的方法,所以发送者总是一个按钮。
  • @Benjamin 这是我遵循的好习惯。 github.com/apple/swift-evolution/blob/master/proposals/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2016-08-16
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
相关资源
最近更新 更多