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?
4 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
- A reference to the tableView it is being used in.
- 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
}

- 3,895
- 4
- 45
- 61

- 2,832
- 1
- 20
- 26
-
Don't forget that you need to set the direction for the gesture recognizer when first initializing it to add it to the cell. Something like this: swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight; – Chris Klingler Dec 05 '13 at 20:26
-
Apple, damn you for not providing `(void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector`. – Ricardo Sanchez-Saez Mar 10 '14 at 17:24
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.

- 3,605
- 2
- 29
- 38
implement
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
these two methods,if the UITableViewCellEditingStyle is UITableViewCellEditingStyleDelete, then do sth to erase your cell with
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
that's all.

- 135
- 1
- 4
for that u have to add this code in your project .
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
}
}
Else mor info u can go through this link enter link description here

- 772
- 1
- 13
- 25
-
Vivek2012 and iWill - Thanks for you replies but i dont want to show a DELETE button, there are already answers to those in stackoverflow. I want the cell to be erased off the minute it receives a swipe left or right gesture. please read my description thanks – carbonr Jan 30 '12 at 06:54