在编辑模式下隐藏UITableView多重选择中的复选标记

Ner*_*ken 4 editmode uitableview ios

我有一个UITableView在编辑模式下使用以下行自动设置多个选择viewDidLoad:

self.tableView.allowsMultipleSelectionDuringEditing = YES;
[self setEditing:YES animated:YES];
Run Code Online (Sandbox Code Playgroud)

但是,我想通过更改其背景颜色来指示选择行,而不是通过自动显示在每行左侧的复选标记来选择.(例如,在邮件应用程序中编辑电子邮件列表时出现的,或在此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;
}
Run Code Online (Sandbox Code Playgroud)

这是UITableView我到目前为止唯一的方法,所以其他一切都应该是默认行为.

在编辑模式下,是否有人知道如何在左侧隐藏这些复选标记?我在单元的附件部分看到了很多关于复选标记的问题,但据我了解,这是另一回事.我也看到人们谈论这个tableView:didSelectRowAtIndexPath:方法,但是当表进入编辑模式时会创建这些复选标记,当用户点击"完成"时会关闭这些复选标记,因此该方法似乎不相关.

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

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

但这只是防止单元格的内容缩进,以便为复选标记腾出空间.复选标记仍然出现.

当然有一种隐藏这些复选标记的方法,并且仍然允许在编辑模式下进行多项选择?或者,UITableView在启用了多个选择的编辑模式下,这些检查标记是否是严格的强制行为?

编辑:我(不情愿地)打开那些有点黑客的答案,比如移动复选标记的框架,直到它离开屏幕.此应用程序供内部使用,无需获得App Store批准.但鉴于在UITableView进入编辑模式时自动创建了复选标记,我甚至不知道如何将它们作为要更改的对象.任何帮助,将不胜感激!

Raz*_*van 6

你必须继承你的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
Run Code Online (Sandbox Code Playgroud)

完成后,转到Inteface Builder并让您的单元格成为该类的一部分MyCustomCell.

您在IB MyCustomCell类,导入你的一部分后,MyCustomCell.hUITableViewController和修改代码如下:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

{
            return UITableViewCellEditingStyleNone;
}
Run Code Online (Sandbox Code Playgroud)

但是你会让你的细胞缩进.要删除该缩进,您必须继承Cell.

这样做你应该很好!我刚试过它,它的工作方式就是你想要的!