长按时出现 iOS 7 UITableViewCell 按钮

标签 ios uitableview

我有一个 UITableViewCell,内容 View 中有 3 个按钮。当我向左滑动时,将显示 3 个按钮。

但是我发现当我长按cell时,它变成了透明的,并且背景显示了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: 的默认实现会删除所有没有选中/突出显示状态的 View 的背景。

在您的情况下,您可以将单元格 selectionStyle 设置为 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];
}   

更新

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

关于长按时出现 iOS 7 UITableViewCell 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087101/

相关文章:

iphone - iPhone 和 ASP.NET WS (XML/JSON) 之间的身份验证

iOS:Swift 中 UITableViewCell 的垃圾桶删除按钮

ios - 无法更改表格 View 框架

ios - 如何在编辑时对 UITableViewCell 进行动画处理?

ios - Apple Metal 中的 PBR 渲染

ios - Core Data iOS 应用程序在获取上下文时崩溃

ios - 如何在 Xcode 上删除 iOS Team Provisioning Profile/AppID?

ios - Facebook 聊天 API 和表情符号

ios - 带有自定义 UITableViewCell 的 UITableView 不显示单元格

ios - 在 UITableView ios 的每个部分中选择一行?