12

我想在UITableViewCell点击时使用默认突出显示。但是,我不希望自定义子视图(及其子视图)接收消息以更新其突出显示的状态并因此破坏backgroundColor属性。

编辑
“子视图”是指任何子UIView类,而不仅仅是UITableViewCells。

也许这种假设情况会更好地表达我在寻找什么:我有一个UITableViewCell。称之为c。然后我添加一个 UIView (称之为v)作为c的子视图。当我点击c时,我希望c突出显示(标准蓝色背景和白色字体颜色),但我不希望v突出显示。我该如何做到这一点?

4

12 回答 12

6

首先,UITableView枚举所有的子视图,并向它们发送高亮消息。

因此,即使您在视图中放置了UILabel,无论它有多深,它都会遍历所有视图(通过使用 subviews 属性)。

一种解决方案可以是(即 IOS4+),覆盖subviews属性,并欺骗 tableview 的高亮功能,我们没有任何子视图。为此,我们需要确定调用者,如果它是 tableview 的 highlight 方法,我们应该根本不返回子视图。

我们可以创建一个简单的UIView子类并覆盖下面的子视图

- (NSArray *)subviews{
    NSString* backtrace = [NSString stringWithFormat: @"%@",[NSThread callStackSymbols]];
    if ([backtrace rangeOfString:@"_updateHighlightColorsForView"].location!=NSNotFound)
        return [super subviews];   

    return [[NSArray new] autorelease];
}
  • callStackSymbols在 IOS4+ 之后可用
  • _updateHighlightColorsForView是UITableView的方法,负责高亮所有的children
于 2011-07-12T21:00:39.853 回答
6

我有一个继承自的类UITableViewCell,所以我修复了它,setSelected:animated如下所示:

   - (void)setSelected:(BOOL)selected animated:(BOOL)animated
   {
       [super setSelected:selected animated:animated]; 
       //Reset highlighted status to all the childs that i care for.
       [self.someChild setHighlighted:NO];
       [self.someOtherChild setHighlighted:NO];
   }
于 2013-03-21T18:42:31.483 回答
3

在表格单元格上使用 UITableViewCellSelectionStyleNone。

请参阅苹果 API 文档

于 2011-06-20T21:52:28.803 回答
3

我通过这样的覆盖解决了这个问题-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated(仅当子视图是 UIControl 的子类时才有效):

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    BOOL subviewWasEnabled = mySubview.enabled; //check if the view is enabled
    mySubview.enabled = NO; //disable it anyways

    [super setHighlighted:highlighted animated:animated];

    mySubview.enabled = subviewWasEnabled; //enable it again (if it was enabled)
}
于 2011-08-13T21:58:55.220 回答
1

最近我一直在寻找同样的方法,老实说,我找不到以编程方式执行此操作的方法,所以我有点作弊了。

由于突出显示尊重图像,我最终使用UIImageView归档整个框架UIView并为其分配空白图像并在UIImageView. 您UIView仍然会突出显示,但您将无法看到它,因为上面会有一个空白图像!

我希望这会有所帮助,我知道这可能不是最好的方法,但得到了我需要的结果

于 2011-07-08T14:10:38.110 回答
1

覆盖子类-(void)setHighlighted:(BOOL)highlight;中的方法UIControl,使其不执行任何操作。对于UIButton子类,您可以执行以下操作:

- (void)setHighlighted:(BOOL)highlighted {
    return;
}
于 2011-10-31T14:54:35.803 回答
1

我不想突出显示任何子视图。我的解决方案是将 selectionstyle 设置为 UITableViewCellSelectionStyleNone 并模仿默认选择行为如下(在 UITableViewCell 的自定义子类上覆盖 setSelected):

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    int backgroundViewTag = 888;
    UIView *backgroundView = [self viewWithTag:backgroundViewTag];

    if (selected) {
        if (!backgroundView) {
            backgroundView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
            backgroundView.tag = backgroundViewTag;
            backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
            backgroundView.backgroundColor = [UIColor blueColor];
            backgroundView.alpha = 0.0;
            [self insertSubview:backgroundView atIndex:0];
        }

        if (animated) {
            [UIView animateWithDuration:0.5 animations:^{
                backgroundView.alpha = 1.0;
            }];
        } else {
            backgroundView.alpha = 1.0;
        }
    } else if (backgroundView) {
        if (animated) {
            [UIView animateWithDuration:0.5 animations:^{
                backgroundView.alpha = 0.0;
            } completion:^(BOOL finished) {
            }];
        } else {
            backgroundView.alpha = 0.0;
        }
    }
    [super setSelected:selected animated:animated];
}
于 2013-05-24T09:57:38.870 回答
0

也许我们可以更改子视图的突出显示状态以匹配它们的默认视图。

这样,即使它更改为突出显示状态,它也会看起来处于默认状态。

不确定它是否有效,你能更多地描述子视图吗?

于 2011-07-11T14:50:48.660 回答
0

我认为您需要禁用子视图(可能不需要)或子类化子视图以覆盖此行为。

于 2011-07-12T18:19:29.533 回答
0

也许如果您将子视图子类化并覆盖 setHighlighted 方法以不执行任何操作或覆盖突出显示的方法以始终返回 NO。

但是 UIView 没有突出显示的状态,你添加什么样的 UIView 孩子到你的单元格?

于 2011-07-12T23:15:02.010 回答
0

所以也许是这样的:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

m_colors = [[NSMutableArray alloc] init];

for (UIView *view in [cell.contentView subviews]) {
    [m_colors addObject:view.backgroundColor];
}

return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

for (int x = 0; x < [m_colors count]; ++x) {
    [[[cell.contentView subviews] objectAtIndex:x] setBackgroundColor:[m_colors objectAtIndex:x]];
}

[m_colors release];
m_colors = nil;
}
于 2011-07-12T23:53:07.313 回答
0

只需将您的视图放在UIButton中,它们不会在突出显示时更新。您不能仅在运行时使用情节提要的 xib 来做到这一点。

UIButton *button = [[UIButton alloc] initWithFrame:0, 0, cellWidth, cellHeight];
[button addSubview:anyView]; // This view don't change his background color
[cell addSubview:button];
于 2015-10-29T08:26:49.517 回答