44

I have UITableView and I am trying to load it by default in edit mode. The problem is when I this line table.editing=TRUE; my rows disappear, I implemented this methods:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
// Return NO if you do not want the specified item to be editable.
return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
return UITableViewCellEditingStyleDelete;
}


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

but with no luck. What should I do?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user739509
  • 813
  • 3
  • 10
  • 19

5 Answers5

63

as Anish pointed to using

[tableView setEditing: YES animated: YES]; 

But you need to have it in viewWillAppear view event to make it work.

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
10

try this...

[tableView setEditing: YES animated: YES];
Anish
  • 2,889
  • 1
  • 20
  • 45
  • No luck. I also tried realoding data after setting the edit mode: [table reloadData]; – user739509 May 14 '11 at 12:41
  • have a look at here...http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html – Anish May 14 '11 at 12:44
7

In ViewDidLoad write

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(addORDoneRows)];
[self.navigationItem setLeftBarButtonItem:addButton];

addORDoneRow

- (void)addORDoneRows
{
    if(self.editing)
    {
        [super setEditing:NO animated:NO];
        [_dbSongsTblView setEditing:NO animated:NO];
        [_dbSongsTblView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
    }
    else
    {
        [super setEditing:YES animated:YES];
        [_dbSongsTblView setEditing:YES animated:YES];
        [_dbSongsTblView reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
    }
}

For MultipleSelection of Row

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

Note: There are Three Editiing Style as below

UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert

Note: And for multiple Selection set Selection and Editing Style to Multiple from Attribute inspector

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
  • was there a reason to call `reloadData`? In my case it isn't, apart that disables the animation when entering/exiting from edit mode – rgkobashi Sep 10 '21 at 05:38
2

To load a tableView in edit mode you should call setEditing(true, animated: false) in viewDidLoad().

If your view controller is a subclass of UITableViewController there's no need to change, just make the above call. Otherwise if your view controller is a subclass of UIViewController, then you should make a call in this way: tableView.setEditing(true, animated: true).

Tested with Swift 2.2.

Andrej
  • 7,266
  • 4
  • 38
  • 57
0

[self.tableView setEditing:!self.tableView.isEditing animated:YES];

George
  • 202
  • 2
  • 8