开发者

Prevent custom AccessoryView from showing selection when it's UITableViewCell is selected

开发者 https://www.devze.com 2023-02-01 12:27 出处:网络
To reproduce this, create a UITableView that contains cells with custom AccessoryViews (such as buttons to perform a specific action where touching the other part of the UITableViewCell should per开发

To reproduce this, create a UITableView that contains cells with custom AccessoryViews (such as buttons to perform a specific action where touching the other part of the UITableViewCell should per开发者_开发问答form a different action).

If you touch (select) the UITableView, the AccessoryView shows selection (as thought it was touched) as well. I want to prevent this and only show the AccessoryView's selected state when they actually touch the AccessoryView.

Thanks in advance,

groomsy


When UIButton is set as the accessoryView of a UITableViewCell, setHighlighted will be called for the accessoryView (the UIButton in this case) when its superview (the UITableViewCell) is selected.

To fix this, we need to subclass UIButton, override its setHighlighted setter to ignore if its superview isSelected or isHighlighted.

AccessoryViewUIButton.m

#import "AccessoryViewUIButton.h"


@implementation AccessoryViewUIButton

// Subclass only works for buttonWithType:custom

- (id)initWithFrame:(CGRect)aRect
{
    // Call the superclass's designated initializer 
    self = [super initWithFrame:aRect];

    return self;
}

- (void)setHighlighted:(BOOL)isHighlighted {

    /* Overridden to do nothing if superview is selected or highlighted */

    UITableViewCell* theCell = (UITableViewCell*) self.superview;

    if ([self.superview isKindOfClass:[UITableViewCell class]]) {
        if ([theCell isSelected] || [theCell isHighlighted])
            return;
    }

    [super setHighlighted:isHighlighted];
}

- (void)dealloc {
    [super dealloc];
}


@end


Are you using a custom UITableViewCell subclass? I would try doing so and overriding setSelected:(BOOL)selected for that class to make sure things are handled as you want.


Accessory View would not glow or show selection when the table cell is tapped. I think you want the tableViewCell's blue color selection state to not be visible on the accessoryViews background. is that correct?

I would suggest creating your own custom tableViewCell and setting the cell's selectionStyle to UITableViewCellSelectionStyleNone and handling the tableRowSelection to setSelected state for the cell side alone and not the accessory view.

or just make the accessory view's background a little larger and dont set it backgroundColor to clearColor. that way the selection state of the cell wont be shown on the accessoryView as well.


Subclass the table view cell, and override the following methods:

- (void) setHighlighted: (BOOL) highlighted;
- (void) setHighlighted: (BOOL) highlighted
               animated: (BOOL) animated;
- (void) setSelected: (BOOL) selected;
- (void) setSelected: (BOOL) selected
            animated: (BOOL) animated;

and make sure the button's selected and highlighted states are reset to NO after calling the superclass method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号