Question

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.

Was it helpful?

Solution

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;
}

OTHER TIPS

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
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top