繁体   English   中英

iOS 7 UITableViewCell按钮出现在长按

[英]iOS 7 UITableViewCell button appeared at long press

我在内容视图中有一个带有3个按钮的UITableViewCell 当我向左滑动时,将显示3个按钮。

但是当我长按细胞时,我发现它变得透明了,3个按钮显示在背景中。 这是一个问题吗?

当长按单元格时,我可以修改代码使按钮不可见吗?

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self.contentView addSubview:self.thumbnailButton];
        [self.contentView addSubview:self.renameButton];
        [self.contentView addSubview:self.deleteButton];
        [self.contentView addSubview:self.containerView];
        [self.containerView addSubview:self.seperator];
        [self.containerView addSubview:self.thumbnailImageView];
        [self.containerView addSubview:self.nameLabel];
        [self.containerView addSubview:self.ipLabel];
    }
    return self;
}

- (void)swipe:(UISwipeGestureRecognizer *)recognizer
{
    BOOL canShow = [self.delegate cellMenuWillShow:self];

    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        if (!canShow) {
            [self hideMenu];
        }

        return;
    }

    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        if (!canShow) {
            return;
        }
    }

    [UIView animateWithDuration:.3 animations:^{
        CGRect frame = self.containerView.frame;
        frame.origin.x -= 250;
        self.containerView.frame = frame;
    } completion:^(BOOL finished) {
        self.menuShowed = YES;
        if ([self.delegate respondsToSelector:@selector(cellMenuDidShowed:)]) {
            [self.delegate cellMenuDidShowed:self];
        }
    }];
}

您看到的长抽头行为是UITableViewCell突出显示。 -setHighlighted:animated:-setSelected:animated:的默认实现-setSelected:animated:删除没有选定/突出显示状态的所有视图的背景。

在您的情况下,您可以将单元格selectionStyle UITableViewCellSelectionStyleNone设置为UITableViewCellSelectionStyleNone

或者,您可以覆盖这两种方法,也可以不调用super实现,或者在super设置所需的背景颜色后立即执行:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    self.contentView.backgroundColor = [UIColor redColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    self.contentView.backgroundColor = [UIColor blueColor];
}   

更新

一些评论WRT你的代码:最好不要处理单元格内的手势识别器。 您可以在UITableView使用一个,这样就可以实现类似iOS7的行为。 例如,当你刷另一个单元格时 - 先前选择的菜单关闭。 如果你的表有很多相同的单元格,你不需要在每个单元格中都有菜单按钮, - 在UITableView级别动态创建菜单,并在它出现之前将它放在单元格下面。

暂无
暂无

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

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