Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

editButtonItem set but no minus buttons?

My edit button is placed in viewDidLoad:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

It shows up correctly on the nav bar, and tapping this button indeed change it to Done. However, no minus buttons show up in my table rows. Swiping a row, then tap Delete works, though.

Any ideas?

EDIT 1: Here's how I'm doing:

- (void)loadView {
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
tableView.delegate = self;
tableView.dataSource = self;
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

self.view = tableView;
}

EDIT 2: My observation is that the edit and minus buttons display fine if my tableview is created in IB (RootViewController). The other two (or three) tableview are created by the aforemention code, so that might be the problem. Guess I'd have to dive in to isEditing, editing and whatnot.

like image 403
Anh Avatar asked May 25 '09 09:05

Anh


3 Answers

If you don't subclass UITableViewController, there is a way to do it. Just implement setEditing:animated: in your UIViewController subclass as follows:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing: editing animated: animated];
    [self.tableView setEditing:editing animated:animated];
}

Note: replace "self.tableView" if needed...

Also add the "edit" buttonto the toolbar:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

And that's all!

like image 71
greydet Avatar answered Nov 10 '22 14:11

greydet


Silly me. I forgot to change UIViewController (the class my view controller inherits from) to UITableViewController. Now, it works.

Without doing this, I would need to enable row editing manually like:

// in loadView
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(turnOnEditing)];

- (void)turnOnEditing {
[self.navigationItem.rightBarButtonItem release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(turnOffEditing)];
[self.tableView setEditing:YES animated:YES];
}

- (void)turnOffEditing {
[self.navigationItem.rightBarButtonItem release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(turnOnEditing)];
[self.tableView setEditing:NO animated:YES];
}
like image 21
Anh Avatar answered Nov 10 '22 14:11

Anh


To show and to activate the minus button at the left corner of each of the tableviewcell, do the following:

Add the following code to your viewdidload method:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

//The above line is to show the "EDIT" and "DONE" button on the top right corner of your navigation bar.

And also add the following method to your .m file to show and animate the MINUS sign on the tableview cells.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
 {
    [super setEditing:editing animated:animated];

    if(editing == YES) {

    [self.tableView setEditing:editing animated:animated];

     } else {

    [self.tableView setEditing:NO animated:animated];

     }

}
like image 30
Md Rais Avatar answered Nov 10 '22 15:11

Md Rais