我有一个带按钮的自定义TableViewCell。在切换到动态单元格之前,此按钮工作正常。但是,现在按钮无法选择。
按钮在触摸时甚至不会突出显示。
最好的部分?如果我禁用AutoLayout,当按钮出现在viewcontroller的最顶部时,几乎太小而无法触摸...它可以正常工作
尝试删除自动布局并以编程方式更改单元格内按钮位置的事件不起作用:
Button.frame.origin.x = self.contentView.frame.width / 2
我按钮的代码:
class HotelRateCell: UITableViewCell {
@IBOutlet weak var hotelRateTitle: UILabel!
@IBOutlet weak var hotelRatePrice: UILabel!
@IBOutlet weak var type: UILabel!
@IBOutlet weak var details: UILabel!
@IBOutlet weak var button: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
//tried both enabled and disabled
self.contentView.userInteractionEnabled = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
实际行动的代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: HotelRateCell = tableView.dequeueReusableCellWithIdentifier("HotelRateCell", forIndexPath: indexPath) as! HotelRateCell
cell.reservationButton.tag = indexPath.row
cell.button.addTarget(self, action: "reservationButton:", forControlEvents: .TouchUpInside)
cell.reservationButton.addTarget(self, action: "reservationButton:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func reservationButton(sender: UIButton!) {
if sender.tag == 0 {
UIApplication.sharedApplication().openURL(NSURL(string: "http://reddit.com")!)
println("rservation button pressed")
}
}