Paul S. Paul S. -4 years ago 117
Objective-C Question

How to get the indexPath of a UITableView Row in edit mode

Is there a way to get a row that is in edit mode?

I know I can get it here

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

but how do I get it out side of this method?

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
doesn't work as it returns NULL...

When I mean edit mode that means the row has shifted to the left and shows Delete at the right hand side...

enter image description here

Thanks in advance.

Answer Source

indexPathForSelectedRow does not work because the row is not selected.

Define a property to hold the current row selected for deletion:

@property (nonatomic, strong) NSIndexPath *indexPathOfDeleteRow;

Then use tableView:commitEditingStyle:forRowAtIndexPath to update the property:

- (void)  tableView:(UITableView *)tableView 
  forRowAtIndexPath:(NSIndexPath *)indexPath
    if (editingStyle == UITableViewCellEditingStyleDelete)
        self.indexPathOfDeleteRow = indexPath;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download