So I want to be able to determine how to differentiate insert operations from delete operations so that I can respond accordingly. Currently I have this code to create a “Done”, “Edit” and “Add” button
- (void)initializeNavigationBarButtons
{
UIBarButtonItem *newEditButton =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self action:@selector(performEdit:)];
self.editButton = newEditButton;
[newEditButton release];
self.navigationItem.rightBarButtonItem = self.editButton;
UIBarButtonItem *newDoneButton =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(performDone:)];
self.doneButton = newDoneButton;
[newDoneButton release];
UIBarButtonItem *newAddButton =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(performAdd:)];
self.addButton = newAddButton;
[newAddButton release];
self.navigationItem.leftBarButtonItem = self.addButton;
}
then I have these 3 as the so-called “callback” functions for the buttons:
- (void)performDone:(id)paramSender
{
[self.tableView setEditing:NO animated:YES];
[self.navigationItem setRightBarButtonItem:self.editButton
animated:YES];
[self.navigationItem setLeftBarButtonItem:self.addButton
animated:YES];
}
- (void)performEdit:(id)paramSender
{
NSLog(@"Callback Called");
[self.tableView setEditing:YES animated:YES];
[self.navigationItem setRightBarButtonItem:self.doneButton
animated:YES];
[self.navigationItem setLeftBarButtonItem:self.doneButton
animated:YES];
}
- (void)performAdd:(id)paramSender
{
[self.tableView setEditing:YES animated:YES];
[self.navigationItem setRightBarButtonItem:self.doneButton
animated:YES];
[self.navigationItem setLeftBarButtonItem:self.doneButton
animated:YES];
}
and here is where I am “supposed to” determine whether it is an add or delete operation:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *output = (isDeleting) ? @"Deleting" : @"Adding";
NSLog(@"%@", output);
UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;
if ([tableView isEqual:self.tableView]){
if (self.isDeleting == YES){
result = UITableViewCellEditingStyleDelete;
}
else{
result = UITableViewCellEditingStyleInsert;
}
}
return result;
}
however, I don’t know where I am supposed to set self.isDeleting and self.isAdding. I tried to set them in the callbacks but it seems that the tableView:cellEditingStyleForRowAtIndexPath: gets called first and in my viewDidLoad the default value for them is NO.
So how do I properly set the values of isAdding and isDeleting for me to be able to act accordingly in the tableView:cellEditingStyleForRowAtIndexPath: method?
Thanks in advance!
If you have an ‘Add’ button in the navigation bar why not just run the ‘add’ action when it’s pressed instead of making the tableview editable? There’s also not much point in setting a cell’s editing style to
UITableViewCellEditingStyleInsertwhen that cell has content. The usual flow is to either have an ‘Add’ button outside the table (e.g: in the navigation bar) which performs the add action, or as the last (or first) cell inside the tableview while it’s in editing style, and when that cell is pressed the add action is performed.So you can either keep the ‘Add’ button in the navigation bar and make all cells editable with
UITableViewCellEditingStyleDelete, or keep only the ‘Edit’/’Done’ buttons and while editing add a new cell (while the tableview is editable) which will be editable withUITableViewCellEditingStyleInsert.Side note: instead of
if ([tableView isEqual:self.tableView])you should better useif (tableView == self.tableView)since you want to check if it’s the same instance, and not if they’re equal.