I just inserted a table into a normal UIViewController and connected the delegate and source components with the file\'s owner. Everything works fine when i insert data into
I could just solve the problem for deleting the rows. To make it work i inserted like ACB told me, the following methods:
//when blue accessory button has been pressed
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//turn into edit mode
[tableView setEditing:YES animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
}
// method to enable the deleting of rows
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
Activity *activity = [allActivities objectAtIndex:indexPath.row];
// remove the item from the array
[allActivities removeObjectAtIndex:indexPath.row];
//delete element from database with created method
[dataController deleteFromTable:@"activity" theElementWithID:activity._id];
[tableView setEditing:NO animated:YES];
// refresh the table view
[tableView reloadData];
}
}
I found this solution on cocoapi.wordpress.com/tag/caneditrowatindexpath
But i still have just one problem. If somebody enters in editing mode, and won't delete a row, there is no possibility, to turn the editing mode off again. If you switch the view and come back again it autmatically stops, but otherwise its not possible because i don't have a NavigationController inserted.
Is there a way, to access the state of the Deletion Control on the left of the row while editing? it would be useful to detect if somebody turned the deletion off again to jump out of editing mode.
It might not fit the apple user guide, but im just on my first project and it should just work.
In order to enable edit mode for table view, you can call edit method on UITableView
as,
[self.tableView setEditing:YES animated:YES];
You have to implement tableView:commitEditingStyle:forRowAtIndexPath:
method to enable the deleting of rows. In order to delete the rows you need to use deleteRowsAtIndexPaths:withRowAnimation:
method.
For eg:-
self.navigationItem.rightBarButtonItem = self.editButtonItem;//set in viewDidLoad
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { //Implement this method
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //implement the delegate method
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Update data source array here, something like [array removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
For more details check the apple documentation here.