How to detect a swipe-to-delete gesture in a customized UITableviewCell?
Asked Answered
F

2

13

I have customized a UITableViewCell and I want to implement "swipe to delete". But I don't want the default delete button. Instead, I want to do something different. What would be the easiest way to implement this? Are there some methods which get called when the user swipes to delete a cell? Can I prevent then the default delete button from appearing?

Right now I think I must implement my own logic to avoid the default delete button and shrink animations which happen in swipe to delete in the default implementation of UITableViewCell.

Maybe I have to use a UIGestureRecognizer?

Fineable answered 29/5, 2011 at 12:37 Comment(0)
J
16

If you want to do something completely different, add a UISwipeGestureRecognizer to each tableview cell.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.


    UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwiped:)];
    [sgr setDirection:UISwipeGestureRecognizerDirectionRight];
    [cell addGestureRecognizer:sgr];
    [sgr release];

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
    // ...
    return cell;
}

- (void)cellSwiped:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view;
        NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
        //..
    }
}
Jodyjoe answered 29/5, 2011 at 12:55 Comment(3)
vmanjz's answer is a lot better due to you not having to create UISwipeGestureRecognizer for each table cell. As with a very large table you could see some serious lag creating that many gesture recognisers.Purism
you could add the gesture recognizer to the table view instead. check out my answer to a similar issue: https://mcmap.net/q/370182/-uigesturerecognizer-and-uitableviewcell-issueJodyjoe
One problem with this approach is that it only recognizes the .Ended state, not the .Began stateDeferential
E
16

Here are two methods that can be used to avoid the Delete Button:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

and

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
Edmea answered 19/8, 2012 at 19:34 Comment(1)
There is also - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath if user doesn't remove the rowIssy

© 2022 - 2024 — McMap. All rights reserved.