tags:

views:

35

answers:

2

ive used the sdk standard code for deleting however it crashes once i press the delete button. i am using this code

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[tableFavoritesData arrayWithObject:indexPath] withRowAnimation:YES];
 }   

 }

i tried with both NSMutableArray instead of tableFavoritesData however nothing works. can anyone help me out?

+1  A: 

Well, basically what you want to do is:

  1. Delete the row from the data source (array).
  2. Tell the table view that you have deleted a row from the data source.

Correct code should probably look like that:

if (editingStyle == UITableViewCellEditingStyleDelete) {
 [tableFavoritesData removeObjectAtIndex:indexPath.row];
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   

EDIT: I didn't notice another error.

You need to specify the type of animation, not just pass YES or NO. For example: UITableViewRowAnimationFade. Check out possible UITableViewRowAnimation values here.

EDIT 2: For the comment below (comment formatting suck): Check out NSNotificationCenter in the docs, especially addObserver:selector:name:object: and postNotificationName:object: methods.

In your other view controller (probably viewDidLoad method):

[[NSNotificationServer defaultCenter] addObserver:self selector:@selector(deletedRow:) name:@"RowDeleted" object:nil];

-(void) deletedRow:(NSNotification*) notification
{
  NSDictionary* userInfo = [notification userInfo];
  NSIndexPath indexPath = [userInfo objectForKey:@"IndexPath"];
 // your code here
}

and while deleting the row:

if (editingStyle == UITableViewCellEditingStyleDelete) {
...
[[NSNotificationServer defaultCenter] postNotificationName:@"RowDeleted" object:self userInfo:[NSDictionary dictionaryWithObject:indexPath forKey:@"IndexPath"]];
     }   

Just remember that you need to remove observer from notification center when you dealloc the other UIViewController:

[[NSNotificationServer defaultCenter] removeObserver: self];

Hope I didn't make much errors, I don't have access to XCode atm.

Estarriol
great thanks a lot.(a sidenote to my question i have an array of data that is copied from another controller to the one that has a table. is there any way to remove an object from that array without having to recopy the entire array and nsuserdefault it again if it is removed?)
Alx
I''ve added some sample code to the answer, couldn't edit my comment as it was edited already. Generally you want to register your other ViewController as an observer, and post a notification to it while deleting the object.
Estarriol
A: 

If you looked at the console, it will most probably tell you that your model (your data structure) doesn't match what the table expects. I.e. your delegate method

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

must return one less than before.

Also, [tableFavoritesData arrayWithObject:indexPath] looks very strange and is probably not what's expected. Maybe you want [NSArray arrayWithObject:indexPath] here. And remove the data from your model first.

Eiko