【发布时间】: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")
}
模拟器:
我错过了一些简单的事情吗?
编辑:
向所有试图提供帮助但被否决的人道歉,因为我遗漏了更多重要信息。所有这些都在我的viewDidLoad 中IncomeFeedVC:
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.button是nil,则尝试使用nil引用时会发生崩溃。
标签: ios swift uitableview uibutton swift3