Hiding checkboxes in multiple selectable UITableView in edit mode

I have UITableView

one that automatically configures for multiple selection in edit mode using the following lines in viewDidLoad

:

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

      

However, I would like to point out that the row was selected by changing the background color rather than using the checkboxes that automatically appear to the left of each row. (For example, the ones that show up when editing an email list in the Mail app, or are discussed in this SO question ). It works for me for most except that I can't get those checkboxes that are automatically created as part of putting UITableView

into edit mode to go away.

Below is the code I am working with:

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

      

These are the only methods UITableView

I have so far, so everything else should be default.

Does anyone know how to hide these checkboxes on the left, in edit mode? I've seen a lot of questions about checkboxes in the accessory part of the cell, but as I understand it, this is a completely different matter. I've also seen people talk about the method tableView:didSelectRowAtIndexPath:

, but these checkboxes are generated when the table goes into edit mode and fired when the user clicks Done, so the method doesn't seem to be related.

The closest I've come to find this method:

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

      

But this simply prevents the cell content from being selected to make room for the checkboxes. The icons are still displayed.

Surely there is a way to hide these checkboxes and still allow multiple selection in edit mode? Or do these checkboxes have serious mandatory behavior for UITableView

editing mode with multiple selection enabled?

EDIT: I (reluctantly) open up answers that are a bit of a hack, like moving the checkbox box around until it appears on the screen. This app is for internal use only and does not need App Store approval. But given that the checkboxes are created automatically when UITableView

it goes into edit mode, I don't even know how to get them as objects to modify. Any help would be appreciated!

+3


source to share


3 answers


You will have to subclass your UITableViewCell and override the method (void)setEditing:animated:

like this:

#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

      

After that go to Inteface Builder

and make your cell part of a class MyCustomCell

.

After you make your MyCustomCell class cell in IB, import MyCustomCell.h

into yours UITableViewController

and change the following in your code:

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

      



UPDATE: You can also do the following in your TableView tableView:editingStyleForRowAtIndexPath:

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

{
            return UITableViewCellEditingStyleNone;
}

      

But you will get your indemnity cell. To remove this indentation, you will have to subclass Cell.

You should be good to go after that! I just tested it and it works the way you want it to.

+4


source


Here is the simplest multiple choice solution without checkboxes:

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

      

This will cause the cells to be selected using the default selection style (gray or using a custom selection background) and no checkmarks appear.




A word on whatever solution you choose. Users expect a consistent experience across multiple applications, and these checkboxes are part of that sequence. Make sure you have a good reason to change the look of your regular OS.

0


source


here's how to achieve:

  • swipe to delete works
  • you don't see the checkbox, delete the item or anything else to the left of the cell when in edit mode
  • Cell indents still work fine (you can turn this off if you like)
  • (bonus) Multiple choice support

so - in your 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)
}

      

you also need to customize your UITableView

self.table.allowsMultipleSelectionDuringEditing=NO;

      

now if you really need multiple choice;

self.table.allowsSelectionDuringEditing=YES;

      

then manage the selected cells yourself.

I am placing a custom checkbox in my UITableViewCell subclass and also changing the value in response to

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

      

The questionnaire wants to indicate the highlighting with the background color - this part should be simple.

0


source







All Articles