I want to add insert and delete buttons for individual cells in a table view.
The following code is working fine for deleting for insertion also I want the button not in navigation.
- (void)viewDidLoad {
[super viewDidLoad];
card = [[NSMutableArray alloc]initWithObjects:@"card1",@"Card2",@"card3",@"card4",nil];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[card removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
To get rid of the edit button in navigation bar get rid of this line:
Then create your own button and with its action call
[tableview setEditing:]yourself.To enable delete buttons for specific cells and not others, implement the tableview delegate method:
And return
UITableViewCellEditingStyleNonefor any rows you don’t want to show a delete button in edit mode, andUITableViewCellEditingStyleDeletefor ones that you do want to.