Which button state is triggered when you long-click (highlight) the cell in which it exists?

262 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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. When UIButton is placed on UITableViewCell then table controls your UIButton: on cell touch down (long touch) UIButton as well as UITableViewCell change its state to Highlighted and returns to Default or Selected 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:

- (void)setHighlighted:(BOOL)highlighted;

Example:

@interface XButton : UIButton
@end

@implementation XButton
- (void)setHighlighted:(BOOL)highlighted { }
@end

This should prevent state changing by table.

2
On

when you click and hold a button the state that is getting called is the highlighted state, thus when you release the click the state returns to UIControlStateNormal