简体   繁体   中英

Get the indexpath of the selected cell

When user taps on the default delete button in UITableView Edit mode,The user should get an alertview and if he hits Delete again in the AlertView the row should get deleted.Initially I have done this with the following code without the AlertView and it worked fine.

  [[self categoriesArray]removeObjectAtIndex:[indexPath row]];
   NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
   [self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
   [self.categoriesArray writeToFile:[self dataFilePath]  atomically:YES];

But now,as i have to use the same code in alertview delegate method.i dont know how to get the [indexPath row]

Set your indexpath to the UIAlertView Tag and get from Delegate.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
       UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Are you sure want to delete" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil];
       [alertView setTag:indexPath.row]; // Assigning here.
        [alertView show];
    }
}
    // UIAlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%d",alertView.tag); // Your Indexpath is here

**Edited:**

    NSIndexPath * path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
    [[self categoriesArray]removeObjectAtIndex:[path row]];
    NSArray * indexPathsToRemove = [NSArray arrayWithObject:path];
    [self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationLeft];
    [self.categoriesArray writeToFile:[self dataFilePath]  atomically:YES];
}

Set the tag to alert view on commitEditingStyle method as

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath // indexPath is here
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Delete Record" message:@"Are you sure to delete the record." delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
       alert.tag = indexPath.row;
       [alert show];
    }
}

On Alert delegate method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     if(buttonIndex == 1)//YES button clicked
     {
         [[self categoriesArray]removeObjectAtIndex:alertView.tag];
     }
}

UITableView makes this information available to you via:

- (NSIndexPath *)indexPathForSelectedRow

or if multiple selections are made:

- (NSArray *)indexPathsForSelectedRows

Use alert view inside this delegate method

  - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

and then do whatever you want to do if the user clicks delete. if the user pressess delete in the alertview then you have to use alert view delegate method to handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM