سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top