Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Antonio Cancio
1,337 PointsUIButton within UITableViewCell not registering taps
I have a custom subclass of UITableViewCell that contains a UIButton for which I want a certain action. When I tap on the button, though, nothing happens. The table cell is still selectable, and when I hold down the button, it turns to the UIControlState.Highlighted icon for the button.
How do I debug this?
This is my subclass:
class ServerSelectionTableViewCell: UITableViewCell {
let isSelected = UIImage(named: "star-filled.png")
let unselected = UIImage(named: "star.png")
var favoriteButton: UIButton? = nil
var server : Server? = nil {
didSet{
if let theServer = server {
var server : String? = theServer.serverCode
self.textLabel?.text = serverCode
server?.isActive() { success in
self.accessoryType = success ? .Checkmark : .None
}
}
}
}
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
self.detailTextLabel?.textColor = UIColor.grayColor()
self.favoriteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
self.favoriteButton?.setImage(unselected, forState: self.favoriteButton?.setImage(unselected, forState: UIControlState.Normal)
self.favoriteButton?.setImage(isSelected, forState: UIControlState.Highlighted)
self.favoriteButton?.setImage(isSelected, forState: UIControlState.Selected)
self.favoriteButton?.addTarget(self, action: Selector("buttonAction:"), forControlEvents: UIControlEvents.TouchUpInside)
self.favoriteButton?.userInteractionEnabled = true
self.contentView.addSubview(self.favoriteButton!)
}
func buttonAction(sender:UIButton!) {
println("Button tapped")
}
override func layoutSubviews() {
super.layoutSubviews()
self.favoriteButton?.frame = CGRectMake(10, 5, 35, 35)
self.textLabel?.frame = CGRectMake(55, 5, self.bounds.width, self.bounds.height)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}