簡體   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