I have a custom table view cell view that contains a button. I initialize the button to setBackgroundImage
to an empty circle in its normal state. I also setImage
to a checkmark image for its selected state.
checkmarkButton = [[UIButton alloc] initWithFrame:CGRectMake(kLeftMargin, kTopMargin, kButtonSize, kButtonSize)];
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"empty-circle.png"] forState:UIControlStateNormal];
[checkmarkButton setImage:[UIImage imageNamed:@"checkmark.png"] forState:UIControlStateSelected];
[checkmarkButton addTarget:self action:@selector(checkmarkButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:checkmarkButton];
When the button is clicked on, I set the button as selected and told to redraw so that the button looks like a circle with a checkmark in it. Click on it again and selected
is set to NO
and the cell is told to redraw so that it's an empty circle.
- (void)checkmarkButtonPressed:(id)sender
{
[checkmarkButton setSelected:!checkmarkButton.selected];
[managedObjectContext save:nil];
}
My problem is that this button exists in a custom table view cell view, which seems to control how it's drawn in a way that I can't identify. When I long-click (touch down in the cell and staying in the cell without touching up for a while) on a cell whose button is set selected (should display a checkmark in a circle), the checkmark disappears until I touch up. When I touch up, the state of the cell is correct. It's just wrong while I am long-clicking the cell.
So, how do I control how that button is drawn when I long-click on the cell?
I'm not sure if this is exactly your case because I didn't see your sources. But please check the
Highlighted
state of your button. WhenUIButton
is placed onUITableViewCell
then table controls your UIButton: on cell touch down (long touch)UIButton
as well asUITableViewCell
change its state toHighlighted
and returns toDefault
orSelected
state on touch up.UPDATE: I agree it is unexpected behavior. You can create subclass of UIButton and reimplement this call to leave it empty to prevent calling super method:
Example:
This should prevent state changing by table.