当选择UITableViewCell时,防止自定义AccessoryView显示选择

gro*_*msy 2 iphone objective-c uitableview accessoryview

要重现这一点,请创建一个UITableView,其中包含具有自定义AccessoryViews的单元格(例如,用于执行特定操作的按钮,其中触摸UITableViewCell的其他部分应执行不同的操作).

如果您触摸(选择)UITableView,AccessoryView也会显示选择(如同触摸的那样).我想防止这种情况,只在实际触及AccessoryView时显示AccessoryView的选定状态.

提前致谢,

groomsy

jun*_*jie 5

当UIButton被设置为UITableViewCell的accessoryView时,当选择了superview(UITableViewCell)时,将为accessoryView(在本例中为UIButton)调用setHighlighted.

为了解决这个问题,我们需要子类UIButton,覆盖它的setHighlighted setter,以忽略它的superview是isSelected还是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
Run Code Online (Sandbox Code Playgroud)