如何在 UITableView 的编辑模式下只显示复选标记选择而不选择整个 UITableViewCell?

How to show only the checkmark selection without the selection of entire UITableViewCell in edit mode of UITableView?

我有一个带有自定义 UITableViewCells 的 TableView。我为我的表格视图单元格启用了 UILongPressGestureRecognizer。长按手势识别器,我想编辑我的表格视图。现在在表格视图的编辑模式下,所有 selected 的单元格都与附件复选标记按钮一起 selected。我只想 select 编辑附件复选标记按钮而不是整个单元格。我尝试了多个选项,例如将单元格的 selectionStyle 属性 设置为 UITableViewCellSelectionStyleNone 。在这种情况下,我无法 select 单元格处于编辑模式。

-(void)addLongPressGestureRecognizerForCell:(UITableViewCell *)tableViewCell{

    UILongPressGestureRecognizer *lLongPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureFunction:)];
    lLongPressGesture.minimumPressDuration = 0.3;
    lLongPressGesture.delegate = self;
    [tableViewCell addGestureRecognizer:lLongPressGesture];
    [lLongPressGesture setCancelsTouchesInView:NO];
}

如何实现这个功能?

终于找到答案了!!!

我只需要覆盖 setSelected 方法并在我的自定义 tableViewCell class 中为我的 tableViewCell 更改 selectedBackgroundView .

首先,我在 cellForRowAtIndexPath 方法中为我的 tableViewCell 添加了背景视图。

lCell.selectedBackgroundView = [[UIView alloc] init];

接下来我重写了 setSelected 方法,如下所述。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state

UIImageView *lBalloonView = [self viewWithTag:102];
[lBalloonView setBackgroundColor:[[UIColor hs_globalTint] colorWithAlphaComponent:0.2]];

UITextView *lMessageTextView = [self viewWithTag:103];
lMessageTextView.backgroundColor    = [UIColor clearColor];

UILabel *lTimeLabel = [self viewWithTag:104];
lTimeLabel.backgroundColor  = [UIColor clearColor];

}

另外需要注意的最重要的一点是更改 tableViewCell 选择样式。

lTableViewCell.selectionStyle = UITableViewCellSelectionStyleGray;

一个更简单的解决方案——并且不需要将背景更改为灰色——是将 UITableViewCellmultipleSelectionBackgroundView 设置为透明框。

在Swift中:

override func awakeFromNib()
{
    super.awakeFromNib()

    // Hide selection box in multi-select mode.
    multipleSelectionBackgroundView = UIView()
    multipleSelectionBackgroundView?.backgroundColor = UIColor.clearColor()
}

在Objective-C中:

- (void)awakeFromNib
{
    [super awakeFromNib];

    // Hide selection box in multi-select mode.
    self.multipleSelectionBackgroundView = [[UIView alloc] init];
    self.multipleSelectionBackgroundView.backgroundColor = [UIColor clearColor];
}