在UITableView的编辑模式下隐藏多选时的勾选标记

7

我有一个UITableView,在编辑模式下自动设置为多选,使用以下代码在viewDidLoad中实现:

self.tableView.allowsMultipleSelectionDuringEditing = YES;
[self setEditing:YES animated:YES];

然而,我想指出的是,选中一行的方式是通过更改其背景颜色,而不是通过自动出现在每行左侧的复选框来实现的。(例如,在邮件应用中编辑电子邮件列表时出现的复选框,或在这个SO问题中讨论的复选框。) 我已经基本上解决了它,除了无法让那些由于将UITableView设置为编辑模式而自动创建的复选框消失。
以下是我正在使用的代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _Hierachy.cellCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(testCell == nil) {
        testCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [[testCell textLabel] setText:@"Test Cell"];

    return testCell;
}

这些是我目前所得到的唯一的UITableView方法,因此其他所有内容都应该是默认行为。

有人知道如何在编辑模式下隐藏左侧的勾选标记吗?我看到很多关于单元格附件部分中勾选标记的问题,但据我了解,这是一个不同的事情。我也看到有人谈论tableView:didSelectRowAtIndexPath:方法,但这些勾选标记是在表格进入编辑模式时创建的,并在用户点击“完成”时消失,因此该方法似乎与此无关。

我最接近的方法是找到这个方法:

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

但这只是防止单元格的内容缩进以便为复选标记腾出空间。复选标记仍然会显示。
肯定有一种方法可以隐藏这些复选标记,同时允许在编辑模式下进行多个选择吧?或者说这些复选标记是不可避免的UITableView带有启用多重选择的编辑模式的行为?
编辑:我(勉强)愿意接受某种程度上是hack-y的答案,例如将复选标记的框架移动到屏幕之外。这个应用程序是供内部使用的,不需要经过App Store的批准。但由于当UITableView进入编辑模式时会自动创建复选标记,因此我甚至不知道如何将它们作为对象来修改。任何帮助都将不胜感激!
3个回答

10
你需要子类化你的UITableViewCell并覆盖(void)setEditing:animated:方法,像这样:
#import "MyCustomCell.h"

@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

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

    // Configure the view for the selected state
}

- (void)setSelectedBackgroundView:(UIView *)selectedBackgroundView
{
    //Cell Selected Color: CLEAR
    [super setSelectedBackgroundView:selectedBackgroundView];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    //Cell Edit Mode NO Indent & Selected Color: CLEAR
    [super setEditing:NO animated:animated];
    [self setNeedsLayout];
}

@end

完成后,前往Interface Builder将您的单元格设为MyCustomCell类的一部分。

在IB中将您的单元格设置为MyCustomCell类的一部分后,在UITableViewController中导入MyCustomCell.h并修改代码中以下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *testCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(testCell == nil) {
        testCell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [[testCell textLabel] setText:@"Test Cell"];

    return testCell;
}

更新: 您还可以在 TableView 的 tableView:editingStyleForRowAtIndexPath: 方法中执行以下操作:

UPDATE: 您还可以在 TableView 的 tableView:editingStyleForRowAtIndexPath: 方法中执行以下操作:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
            return UITableViewCellEditingStyleNone;
}

但是你的单元格会有缩进。要删除这个缩进,您必须子类化单元格。

在执行此操作后,您应该可以顺利进行!我刚刚测试了它,它可以按照您想要的方式工作!


非常好,谢谢!在选择期间,我确实遇到了单元格之间分隔符的奇怪删除(如此SO问题所述),但是通过这个答案和其中一个答案,我已经完美地解决了它!谢谢! :) 我会尽快确认赏金(22小时内)。 - Nerrolken
哦,我遇到了分隔符不消失的问题(自定义背景图片):)。我不得不快速枚举单元格的子视图才能解决它。我很高兴你已经解决了! - Razvan
好的!谢谢。 - Thomás Pereira

1

这是最简单的多选方案,没有勾选标记:

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {
    return NO;
}

这将导致单元格以默认选择样式(灰色或使用自定义选择背景)进行选择,并且不会出现复选标记。

关于您选择的解决方案,需要注意一点。用户希望在多个应用程序中获得一致的体验,而这些勾选框是此一致性的一部分。确保有充分的理由改变操作系统的外观和感觉。


1
这不是一个有效的答案!这样做将不再允许删除单元格! - Razvan
1
@codeFi,我在问题中没有看到删除单元格的要求,只有多选。 - Léo Natan
2
文档:该方法允许数据源排除个别行不被视为可编辑。可编辑的行在其单元格中显示插入或删除控件。如果未实现此方法,则假定所有行均可编辑。不可编辑的行忽略UITableViewCell对象的editingStyle属性,并且不进行缩进以进行删除或插入控件。可编辑但不想显示插入或删除控件的行可以从tableView:editingStyleForRowAtIndexPath:委托返回UITableViewCellEditingStyleNone... - Razvan
我明白你的意思。但通常可编辑单元格也应该启用删除功能。 - Razvan

0

以下是实现方法:

  1. 滑动删除功能可用
  2. 在编辑模式下,左侧不会显示复选框、删除项或其他任何内容
  3. 单元格缩进仍然正常工作(如果需要,可以关闭此功能)
  4. (额外加分项)支持多选

因此,在您的 UITableViewDelegate 中:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isEditing)
    {
      return UITableViewCellEditingStyleNone;
    }
    else
    {
      return UITableViewCellEditingStyleDelete;
    } 
}

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
  (do stuff)
}

你还需要配置你的UITableView

self.table.allowsMultipleSelectionDuringEditing=NO;

现在如果你确实需要多选;

self.table.allowsSelectionDuringEditing=YES;

然后自己管理所选单元格。

我在我的UITableViewCell子类中放置了一个自定义复选框,并且我还根据响应更改了该值

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

问题提出者想要通过背景颜色来表示选择 - 这部分应该很简单。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接