4

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

Alex Cio
  • 6,014
  • 5
  • 44
  • 74

2 Answers2

10

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.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • ok. i just inserted your methods like you told me, and until the method "deleteRowAtIndexPaths" will be called, then the app crashes. I inserted an [allActivities objectAtIndex:indexPath.row]. The methods arrayWithObject or removeObjectAtIndex are not available. Don't know what to do – Alex Cio Jan 04 '13 at 18:41
  • What is `allActivities`? Change it to `NSMutableArray` everywhere if it is an `NSArray`. And then do `[allActivities removeObjectAtIndex:indexPath.row];` Also do you have a navigation bar to put that button? Otherwise you need to create your own button and set the action method to call `[self.tableView setEditing:editing animated:YES]; }` – iDev Jan 04 '13 at 18:42
  • allActivities is an array, filled with entries of a database. I changed it and know i can access this method, but know the error message "Sending 'void' to parameter of incompatible type 'NSArray *'. – Alex Cio Jan 04 '13 at 18:51
  • Try to debug that and fix that. You are missing something there. Anyways the basic thing is, you need to use an NSMutableArray in place of NSArray to do this. So you might have to fix all compiler warnings before running the code. – iDev Jan 04 '13 at 19:08
  • I looked at the NSArray.h and there are only -(void)-methods. So every method that will be called by an NSMutableArray method doesn't return values that can be passed through to "deleteRowsAtIndexPaths". Only NSArray methods can return values. The next thing is, i just need, to get the row that has been selected from the user. So is it necessary to call the tableview-method instead calling other methods inside tableview:commitEditingStyle? – Alex Cio Jan 04 '13 at 19:16
  • 1
    I didnt understand your first comment. NSMutableArray is subclassed from NSArray and it supports all features of NSArray. So that comment is not true. If you need to delete a row, you need to implement this delegate method. Check the apple documentation for more details http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW19 – iDev Jan 04 '13 at 19:18
1

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.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • 2
    First of all my downvote was for posting a wrong answer. I am not sure why my earlier comment was removed. I had mentioned in that, for deleting you need to use deleteRows as shown in my answer and accessory button functionality is not for enabling the edit mode. Your answer was misguiding someone who is looking for the correct answer. And that has nothing to do with my answer. – iDev Jan 07 '13 at 18:47
  • Second point is, if you feel that my answer didnt work you could have posted a comment below my answer. All you posted was something about void methods in NSArray(I am not sure how it is related to your answer) which didnt make any sense and I had asked for clarification but you didnt reply at all. And you dont have to accept my answer if it didn't help. It is upto you to decide and you can unaccept if you want. I shall delete it then. – iDev Jan 07 '13 at 18:48
  • I just wanted to mark your post with a Flag... but after selecting an option it was deleted. If my post works for me, so why you just vote me down? why don't you explain to me whats wrong with it instead of voting it down? for me everything worked very well, and for the table i had to create it worked very well. I am not here to discuss for things like that, also because it doesn't help anybody here, and it would make much more sense you could tell me things through a messenger but not through comments like that expecially things like the table view. – Alex Cio Jan 07 '13 at 19:49
  • So just don't feel affected. I will just delete this post. I could finish what i had to do, and if it is not correct because its not according to the Programming Guidelines for iPhone Apps, i just understand what you want me to say – Alex Cio Jan 07 '13 at 19:51
  • No, i just understood that it might be the way you explained. I also read inside the developer docs which methods should be used. But i tried so many of them and just the code like i send you worked for me. I just have about one month of objectiv-c experience so i don't know how every element works in detail. But before i'll remove my acceptance, please check the several codes. Even its just for a little project at school i would like to know how to make things better, to improve my self. – Alex Cio Jan 07 '13 at 20:21
  • 2
    If you really want help, you should tell what exactly was the issue you faced. Why couldn't you use `[tableView deleteRowsAtIndexPaths:[..` in place of `reloadData`. And why do you want to enable the edit mode on accessory button tap? Instead why dont you just swipe over the cell to enable edit mode using my code? There are a lot of questions. – iDev Jan 07 '13 at 20:39
  • Ok, first, [deleteRowsAtIndexPaths:[.. didn't work because of the error message i wrote you the last on your post. it caused an error message and the deleteRowAtIndexPaths: method couldn't be called. Next i also tried several other methods i could find inside the UITableViewDelegate class but no one did work. so i just found the solution on following page: http://cocoapi.wordpress.com/tag/caneditrowatindexpath/ . Im sure there might be a way because otherwise the method wouldn't exist but even after reading the manual i couldn't call the method in the right way – Alex Cio Jan 07 '13 at 23:24
  • And, i just tried to solve the editing problem with the accessorybutton because i don't know all possibilities. I found the method in the UITableViewDelegate class and found out how it works. if there is a mehtod like swiping it would be perfect, but thatfore i just can use the elements i recognized. Im happy about each new function that could make my app work better. – Alex Cio Jan 07 '13 at 23:28