Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change Table to Edit mode and delete Rows inside a normal ViewController

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.

enter image description here

like image 786
Alex Cio Avatar asked Dec 21 '22 11:12

Alex Cio


2 Answers

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.

like image 166
iDev Avatar answered Jan 23 '23 04:01

iDev


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.

like image 34
Alex Cio Avatar answered Jan 23 '23 03:01

Alex Cio