Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe left or right anywhere in a UITableViewCell to delete the cell with no delete button?

I want to be able to swipe left or right anywhere in a table view cell to erase the cell of with animation without showing the delete button. How can I do this?

like image 709
carbonr Avatar asked Jan 30 '12 06:01

carbonr


2 Answers

I have not tried and implemented this, but I'll give it a shot. First, create a custom UITableViewCell, and let it have 2 properties that you can use

  1. A reference to the tableView it is being used in.
  2. An indexPath to find the cell in the tableView.(Note, this needs to be updated everytime you delete a cell for all cells where this changes)

In your cellForRowAtIndexPath:, where you create the custom cell, set these properties. Also add a UISwipeGestureRecognizer to the cell

cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)];
[cell addGestureRecognizer:swipeGestureRecognizer];
[swipeGestureRecognizer release];

Make sure that the gesture only receives horizontal swipes.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&&
       ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft
        ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES;
}

In your deleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec
{
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec;
    CustomCell *cell=[swipeGestureRecognizer view];
    UITableView *tableView=cell.tableView;
    NSIndexPath *indexPath=cell.indexPath;
    //you can now use these two to perform delete operation
}
like image 56
MadhavanRP Avatar answered Oct 05 '22 01:10

MadhavanRP


The solution posted by @MadhavanRP works, but it is more complex than it needs to be. You could take a simpler approach and create one gesture recognizer that handles all swipes that occur in the table, and then get the location of the swipe to determine what cell the user swiped.

To setup the gesture recognizer:

- (void)setUpLeftSwipe {
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                       action:@selector(swipeLeft:)];
    [recognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.tableView addGestureRecognizer:recognizer];
    recognizer.delegate = self;
}

Call this method in viewDidLoad

To handle the swipe:

- (void)swipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    ... do something with cell now that i have the indexpath, maybe save the world? ...
}

note: your vc must implement the gesture recognizer delegate.

like image 29
Julian B. Avatar answered Oct 05 '22 00:10

Julian B.