【问题标题】:How to show only the checkmark selection without the selection of entire UITableViewCell in edit mode of UITableView?如何在 UITableView 的编辑模式下只显示复选标记选择而不选择整个 UITableViewCell?
【发布时间】:2015-11-27 13:45:11
【问题描述】:

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

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

如何实现这个功能?

【问题讨论】:

  • 识别器方法的邮政编码
  • 也许修改委托调用并手动更改选择样式?

标签: ios iphone tableviewcell edit-tableview


【解决方案1】:

一种更简单且不需要将背景更改为灰色的解决方案是将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];
}

【讨论】:

    【解决方案2】:

    终于知道答案了!!!!

    我只需要重写 setSelected 方法并在我的自定义 tableViewCell 类中更改我的 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;
    

    【讨论】:

    • 嗨 - 如何快速将左侧选中圆圈的颜色更改为不同的颜色?谢谢
    • @SiphoKoza 将单元格的色调更改为选中圆圈所需的颜色。
    • @Sree 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多