自定义单元格中的 UIButton 不起作用
UIButton in custom cell not working
我有一个 button
自定义 cell
class CardTableViewCell: UITableViewCell {
@IBOutlet weak var detailsButton: UIButton!
}
在我的 view controller
然后我这样做
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//.....more code....
openDealCell.detailsButton.addTarget(self, action: "push:", forControlEvents: UIControlEvents.TouchUpInside)
}
然后:
func push(sender:AnyObject) {
println("tap")
}
但是当我点击 button
时没有任何反应。
我做错了什么?
现在我使用与您的代码完全相同的代码创建了演示项目并且它可以工作。请在 GitHub 检查 here。问题肯定出在你没给我们看的其他东西上,那段代码没有任何问题
修复它!问题不在代码中,而是在情节提要中的我的单元格中:
单元格的用户交互未启用,所有其他视图都不允许用户交互。
我有一个 button
自定义 cell
class CardTableViewCell: UITableViewCell {
@IBOutlet weak var detailsButton: UIButton!
}
在我的 view controller
然后我这样做
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//.....more code....
openDealCell.detailsButton.addTarget(self, action: "push:", forControlEvents: UIControlEvents.TouchUpInside)
}
然后:
func push(sender:AnyObject) {
println("tap")
}
但是当我点击 button
时没有任何反应。
我做错了什么?
现在我使用与您的代码完全相同的代码创建了演示项目并且它可以工作。请在 GitHub 检查 here。问题肯定出在你没给我们看的其他东西上,那段代码没有任何问题
修复它!问题不在代码中,而是在情节提要中的我的单元格中:
单元格的用户交互未启用,所有其他视图都不允许用户交互。