我有一个 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/