objective-cxcodeuitableviewsetediting

Table View setEditing for selected cell at indexPath


I want to enable editing for a single cell in a Table View at the selected index of a long press event, everything works except it enables editing for the entire table. How can I enable editing only on the cell that is selected?

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {

    CGPoint p = [gestureRecognizer locationInView:self.savedPropertyTableView];

    NSIndexPath *indexPath = [self.savedPropertyTableView indexPathForRowAtPoint:p];
            if (indexPath == nil) {
            [self setEditing:YES animated:YES];
            NSLog(@"long press on table view but not on a row");
        }

        else {
            [self setEditing:YES animated:YES];
            NSLog(@"long press on table view at row %d", indexPath.row);
        } 
    }

Solution

  • Not a exact solution but same result, i have created a swipe gesture instead and that works like i was hoping (delete a single cell)

    In the Table View Cell:

    UISwipeGestureRecognizer *sgdr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSlideRight:)];
    [sgdr setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.savedPropertyTableView addGestureRecognizer:lpgr];
    

    Then created method:

    -(void)handleSlideRight:(UISwipeGestureRecognizer *)gestureRecognizer {
        CGPoint p = [gestureRecognizer locationInView:self.savedPropertyTableView];
    
        NSIndexPath *indexPath = [self.savedPropertyTableView indexPathForRowAtPoint:p];
        if (indexPath == nil)
        {
            [self setEditing:YES animated:YES];
            NSLog(@"slide on table view but not on a row");
        }
    
        else
        {
            [self setEditing:YES animated:YES];
            NSLog(@"slide on table view at row %d", indexPath.row);
        }
    }