Reputation: 9732
I'm trying to show a UIAlertView
before actually 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 my logs return the correct path.
I have my view delegating the UIAlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%d", _tmpIndexPath.row);
if(buttonIndex == 1)
{
NSLog(@"%d", _tmpIndexPath.row);
}
}
Now I can't figure out why in the clickButtonAtIndex()
I'm getting an error when trying to log _tmpIndexPath.row
*** -[NSIndexPath row]: message sent to deallocated instance 0x12228e00
Upvotes: 4
Views: 5038
Reputation: 4695
Acknowledging the technical answers above, can I suggest that this isn't actually necessary. If you are using the normal ways to delete items from a table view (edit button and swipe the row) then adding a confirmation in to the flow is going to be contradictory to how people will expect the table to behave. The user already has to tap (or swipe) before even gaining access to the delete functionality, so they must already be pretty sure they want to do it.
Upvotes: 1
Reputation: 7074
You can try to do this
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
Upvotes: 3
Reputation: 15213
NSIndexPath
is a NSObject
and you have it autoreleased in your tableView: commitEditingStyle
method so assigning it to your instance variable: _tmpIndexPath = indexPath;
it gets deallocated later. What you need to do is:
_tmpIndexPath = [indexPath copy];
but be careful because every time you are responsible to release your _tmpIndexPath
before setting it again. The cleaner solution is to use properties:
@property (nonatomic, copy) NSIndexPath *tmpIndexPath;
...
self.tmpIndexPath = indexPath;
Upvotes: 1
Reputation: 1121
Do you use ARC? If not try _tmpIndexPath = [indexPath retain]; and don't forget release it later
Upvotes: 0
Reputation: 21221
you will need to retain the indexPath, what is happening is that your indexPath when the alert is dismissed is already deallocated from your system,
Like this
Change
_tmpIndexPath = indexPath;
to
_tmpIndexPath = [indexPath retain];
Upvotes: 5