0

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?

4

2 に答える 2

0

ボタンをクリックしたままにすると、呼び出される状態が強調表示された状態になるため、クリックを離すと、状態は UIControlStateNormal に戻ります

于 2012-06-07T06:59:19.973 に答える
0

私はあなたの情報源を見ていなかったので、これがまさにあなたのケースであるかどうかはわかりません. ただしHighlighted、ボタンの状態を確認してください。がUIButton配置されるとUITableViewCell、テーブルは UIButton を制御します: セルにタッチダウン (ロングタッチ)UIButtonするだけでなくUITableViewCell、状態を変更してHighlightedから戻るDefaultSelected、タッチアップ時に状態に戻ります。

更新: 予期しない動作であることに同意します。UIButton のサブクラスを作成し、この呼び出しを再実装して空のままにして、スーパー メソッドの呼び出しを防ぐことができます。

- (void)setHighlighted:(BOOL)highlighted;

例:

@interface XButton : UIButton
@end

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

これにより、テーブルごとに状態が変化するのを防ぐことができます。

于 2012-05-26T08:06:47.680 に答える