在UITableViewCell内部,我试图实现一个包含图像和文本的按钮.
似乎标准的UIButton无法实现这一点.所以我创建了一个包含UIImageView和UILabel的UIView.
您可以在右侧看到实现,“跟随行程”按钮(“”是UIImageView,“跟随行程”是UILabel)
我现在正试图使这样的UIView(即按钮)可点击,但我找不到办法.
这是我的实现,但它不起作用:
class StationsIntroHeader: UITableViewCell {
@IBOutlet weak var bigButton: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self,action: Selector("followTrip:"))
bigButton.addGestureRecognizer(tap)
}
func followTrip(sender:UITapGestureRecognizer) {
print("tap working")
}
}
我确保在UIVmageView和UILabel上的UIView和OFF上启用了User Interaction Enabled
解决方法
对我来说,如下所示的示例设置完全有效:
class TableViewController: UITableViewController {
override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("CustomCell",forIndexPath: indexPath)
}
}
class CustomCell: UITableViewCell {
@IBOutlet weak var bigButton: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self,action: Selector("bigButtonTapped:"))
bigButton.addGestureRecognizer(tap)
}
func bigButtonTapped(sender: UITapGestureRecognizer) {
print("bigButtonTapped")
}
}
我没有为视图或imageview或标签更改userInteractionEnabled的任何默认值.将我的实施与你的实施进行比较,看看你是否忘记了某些事情…连接插座?
示例项目:https://www.dropbox.com/sh/hpetivhc3gfrapf/AAAf6aJ0zhvRINPFJHD-iMvya?dl=0
编辑您的项目
func tableView(tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCellWithIdentifier("StationsIntroHeader") as! StationsIntroHeader
headerCell.update()
return headerCell
// return headerCell.update().contentView
}