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 the table rows. but now i am trying to find out how rows can be deleted.
I just looked at a lot of other posts, but couldn't find the right solution.
I tried to insert a asseccory button for each row in the table:
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
i even found the method that will be called when the accessory button is pressed:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Accessory pressed");
//[self tableView:tableView willBeginEditingRowAtIndexPath:indexPath];
//[self tableView:nil canEditRowAtIndexPath:indexPath];
//[self setEditing:YES animated:YES];
}
Inside the log the message is printed, but no one of the methods that i tried to call (the commented one) did change the view to the edit mode. How can i solve this problem?
Here is a Screenshot of the UIViewController. I haven't integrated a navigationController.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With