要重现这一点,请创建一个 UITableView,其中包含具有自定义 AccessoryViews 的单元格(例如用于执行特定操作的按钮,其中触摸 UITableViewCell 的其他部分应该执行不同的操作)。
如果你触摸(选择)UITableView,AccessoryView 也会显示选择(就像它被触摸的那样)。我想防止这种情况,并且仅在它们实际触摸 AccessoryView 时才显示 AccessoryView 的选定状态。
提前致谢,
新郎
要重现这一点,请创建一个 UITableView,其中包含具有自定义 AccessoryViews 的单元格(例如用于执行特定操作的按钮,其中触摸 UITableViewCell 的其他部分应该执行不同的操作)。
如果你触摸(选择)UITableView,AccessoryView 也会显示选择(就像它被触摸的那样)。我想防止这种情况,并且仅在它们实际触摸 AccessoryView 时才显示 AccessoryView 的选定状态。
提前致谢,
新郎
当 UIButton 设置为 UITableViewCell 的附件视图时,当它的父视图(UITableViewCell)被选中时,将为附件视图(本例中的 UIButton)调用 setHighlighted。
为了解决这个问题,我们需要继承 UIButton,覆盖它的 setHighlighted 设置器以忽略它的父视图是选中还是 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
您是否使用自定义UITableViewCell
子类?我会尝试这样做并覆盖
setSelected:(BOOL)selected
该类,以确保按您的意愿处理事情。
点击表格单元格时,附件视图不会发光或显示选择。我认为您希望 tableViewCell 的蓝色选择状态在附件视图背景上不可见。那是对的吗?
我建议创建您自己的自定义 tableViewCell 并将单元格的 selectionStyle 设置为 UITableViewCellSelectionStyleNone
并将 tableRowSelection 处理为单独的单元格侧而不是附件视图的 setSelected 状态。
或者只是使附件视图的背景更大一点并且不要将其背景颜色设置为清除颜色。这样单元格的选择状态也不会显示在附件视图上。
子类化表格视图单元格,并覆盖以下方法:
- (void) setHighlighted: (BOOL) highlighted;
- (void) setHighlighted: (BOOL) highlighted
animated: (BOOL) animated;
- (void) setSelected: (BOOL) selected;
- (void) setSelected: (BOOL) selected
animated: (BOOL) animated;
并确保在调用超类方法后将按钮selected
和 highlighted
状态重置为。NO