1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Paul S.
  • 1,342
  • 4
  • 22
  • 43

2 Answers2

2

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 
 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
  forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
        self.indexPathOfDeleteRow = indexPath;
}
memmons
  • 40,222
  • 21
  • 149
  • 183
1

You can use this UITableView extension:

extension UITableView {

    var indexPathForEditingRow: NSIndexPath? {
        return indexPathsForEditingRows.first
    }

    var indexPathsForEditingRows: [NSIndexPath] {
        return visibleCells.flatMap { cell -> NSIndexPath? in
            guard let indexPath = indexPathForCell(cell) where cell.editingStyle != .None else {
                return nil
            }
            return indexPath
        }
    }

}
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118