Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirmation before deleting row

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
like image 799
woutr_be Avatar asked Dec 26 '22 22:12

woutr_be


2 Answers

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];
like image 126
Omar Abdelhafith Avatar answered Jan 16 '23 20:01

Omar Abdelhafith


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

like image 42
Dilip Rajkumar Avatar answered Jan 16 '23 19:01

Dilip Rajkumar