简体   繁体   English

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

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

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 perform a different action). 要重现此问题,请创建一个UITableView,其中包含带有自定义AccessoryViews的单元格(例如在触摸UITableViewCell的其他部分应执行不同操作的情况下执行特定操作的按钮)。

If you touch (select) the UITableView, the AccessoryView shows selection (as thought it was touched) as well. 如果触摸(选择)UITableView,则AccessoryView也会显示选择(认为已被触摸)。 I want to prevent this and only show the AccessoryView's selected state when they actually touch the AccessoryView. 我要防止这种情况,只在他们实际触摸AccessoryView时才显示AccessoryView的选定状态。

Thanks in advance, 提前致谢,

groomsy 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. 如果将UIButton设置为UITableViewCell的附件视图,则在选择其超级视图(UITableViewCell)时,将为该附件视图(在这种情况下为UIButton)调用setHighlighted。

To fix this, we need to subclass UIButton, override its setHighlighted setter to ignore if its superview isSelected or isHighlighted. 为了解决这个问题,我们需要继承UIButton的子类,重写其setHighlighted setter以忽略其父视图isSelected或isHighlighted。

AccessoryViewUIButton.m 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? 您是否在使用自定义UITableViewCell子类? I would try doing so and overriding setSelected:(BOOL)selected for that class to make sure things are handled as you want. 我会尝试这样做,并重写该类的setSelected:(BOOL)selected ,以确保按需处理。

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. 并确保在调用超类方法后将按钮的selected状态 highlighted状态重置为NO

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. 我认为您希望tableViewCell的蓝色选择状态在附件视图背景上不可见。 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. 我建议您创建自己的自定义tableViewCell并将单元格的selectionStyle设置为UITableViewCellSelectionStyleNone并仅针对单元格而非附件视图将tableRowSelection处理为setSelected状态。

or just make the accessory view's background a little larger and dont set it backgroundColor to clearColor. 或者只是将附件视图的背景调大一点,不要将backgroundColor设置为clearColor。 that way the selection state of the cell wont be shown on the accessoryView as well. 这样,单元格的选择状态也不会显示在附件视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM