Change table to edit mode and delete rows inside normal ViewController

I simply inserted the table into a regular UIViewController and connected the delegate and source components to the file owner. Everything works fine when I insert data into the rows of a table. but now I'm trying to figure out how rows can be deleted.

I just looked through many other posts, but couldn't find the right solution.

I tried to insert an asseccory button for each row in the table:

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

I even found a 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]; } 

A message is displayed inside the log, but none of the methods that I tried to call (commented out) changed the view in edit mode. How can I solve this problem?


Here is a screenshot of the UIViewController. I did not integrate navigationController.

enter image description here

+4
source share
2 answers

I could just solve the problem of deleting rows. To make it work, I inserted, as the 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/caneditrowrowdedexpath

But I still have one problem. If someone enters the editing mode and does not delete the line, there is no way to disable the editing mode. If you switch the view and come back again, it will automatically stop, but otherwise it is impossible, because I do not have a built-in navigation controller.

Is there any way to access the state of the delete control to the left of the line during editing? it would be helpful to detect if someone had disabled deletion again to exit edit mode.

This may not be consistent with the Apple user guide, but im is only on my first project, and it should just work.

+1
source

To enable editing mode for table presentation, you can call the editing method on UITableView as,

 [self.tableView setEditing:YES animated:YES]; 

You need to implement the tableView:commitEditingStyle:forRowAtIndexPath: method to enable row deletion. To delete rows, you need to use the deleteRowsAtIndexPaths:withRowAnimation: method.

For instance: -

 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]; } } 

Read more about the documentation here .

+10
source

All Articles