Confirmation before deleting a line

I am trying to show a UIAlertView before deleting a cell from a UITableView

 NSIndexPath *_tmpIndexPath; - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if(editingStyle == UITableViewCellEditingStyleDelete) { _tmpIndexPath = indexPath; NSLog(@"%d", indexPath.row); // 2 NSLog(@"%d", _tmpIndexPath.row); // 2 UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Dete" message:@"Are you sure you want to delete this entry?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil] autorelease]; [alert show]; } } 

So, both of my logs are returning the correct path.

I have my view passing UIAlertView

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"%d", _tmpIndexPath.row); if(buttonIndex == 1) { NSLog(@"%d", _tmpIndexPath.row); } } 

Now I can’t understand why in clickButtonAtIndex() I get an error when trying to register _tmpIndexPath.row

  *** -[NSIndexPath row]: message sent to deallocated instance 0x12228e00 
+4
source share
5 answers

you will need to save indexPath, what happens when your indexPath, when the warning is fired, is already released from your system,

Like this

Edit

 _tmpIndexPath = indexPath; 

to

 _tmpIndexPath = [indexPath retain]; 
+5
source

You can try to do it

  UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Dete" message:@"Are you sure you want to delete this entry?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil] autorelease]; [alert setTag:indexPath.row]; [alert show]; 

So you can get the value as

 [alertView tag] 

In clickedButtonAtIndex

+3
source

NSIndexPath is an NSObject , and you implement it in your tableView: commitEditingStyle , assigning it to your instance variable: _tmpIndexPath = indexPath; he will be released later. What you need to do: _tmpIndexPath = [indexPath copy]; , but be careful, because every time you are responsible for freeing _tmpIndexPath before setting it again. A clean solution is to use the properties:

 @property (nonatomic, copy) NSIndexPath *tmpIndexPath; ... self.tmpIndexPath = indexPath; 
+1
source

Recognizing the technical answers above, can I suggest that this is not really required. If you use the usual methods of removing elements from the table view (edit button and swipe a line), adding confirmation to the stream will be inconsistent with how people expect the table to behave. The user should already press (or miss) before accessing the deletion functionality, so they should already be sure that they will want to do this.

+1
source

Do you use ARC? If you do not try _tmpIndexPath = [indexPath save]; and don't forget to free him later

0
source

All Articles