简体   繁体   中英

iOS 7 UITableViewCell button appeared at long press

I have a UITableViewCell with 3 buttons inside the content view. when I swiped left, the 3 buttons will be shown.

But I found when I long pressed the cell, it became transparent, and the 3 buttons are shown in the background. Is this a problem or not?

Can I modify the code to make the buttons invisible when the cell is long pressed?

- (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];
        }
    }];
}

The long-tap behaviour that you see is UITableViewCell highlighting. The default implementation of both -setHighlighted:animated: and -setSelected:animated: removes backgrounds for all views that do not have selected/highlighted states.

In your case, you can set cell selectionStyle to UITableViewCellSelectionStyleNone .

Alternatively, you can override both methods and either don't call super implementation, or just right after super set the background colour you want:

- (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];
}   

Update

Some comments WRT your code: It is better not handle gesture recognisers inside the cell. You can use one within the UITableView , that way you can achieve iOS7 like behaviour. Eg when you're swiping another cell - previously selected menu closes. And if your table has a lot of the same cells you don't need to have menu buttons in every one, - create the menu dynamically on UITableView level and put it below the cell just before it should appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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