简体   繁体   中英

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?

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
}

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM