I am using xcode 4.2 with storyboard to create an iphone app.
When I press the edit button in the top right corner I would like to have the options to delete the existing rows and see the extra cell (with the green ‘+’ icon) at the top which would allow me to add a new cell.
I have an array which is being populated in the viewDidLoad method using CoreData
I have enabled the settings button
self.navigationItem.rightBarButtonItem = self.editButtonItem;
And implemented the method
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// removing a cell from my array and db here...
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// adding a cell to my array and db here...
}
}
I realise I need to add the cell at some point which I can then edit but it isn’t clear to me where and I am unable to find a explanation on the internet.
Ok, the basic idea is that when the edit button is clicked we’ll show the delete controls next to each row and add a new row with the add control so that users can click it in order to add an entry right? First, since you have the edit button setup already let’s instruct our table that in editing mode we should show an extra row. We do that in our
tableView:numberOfRowsInSection:a_recshere is the array I’ve setup to store our records so you’ll have to switch that out with your own array. Next up we tell ourtableView:cellForRowAtIndexPath:what to do with the extra row:We also want to instruct our table that for that add row we want the add icon:
Butter. Now the super secret kung fu sauce that holds it all together with chopsticks:
Good luck and bon appetit!