1

In my TableView, I allow users to delete their posts using the swipe to delete:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

However, I also allow users to tap the cell to open up a detail view of the post:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // segue and stuff
}

The problem is, when the user swipes to delete, it also triggers the didSelect method and opens up the detail view. How can I make sure didSelectRowAtIndexPath only triggers on taps, while the delete triggers on a drag swipe?

Adam G
  • 1,188
  • 10
  • 24
  • Have you che checked http://stackoverflow.com/questions/15896802/how-to-differentiate-between-user-swipe-and-tap-action ? – ott-- Nov 12 '15 at 22:15

2 Answers2

1

If you're using a UITableViewController + UIStoryboard and connect your detail view segue to push in your cell prototype selection outlet, this works as expected (didSelectRowAtIndexPath is not called when swiping):

@interface TableViewController ()
@property (strong, nonatomic) NSMutableArray *cellTitles;
@end

@implementation TableViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.cellTitles = [@[@"Apple", @"Banana", @"Carrot", @"Millenium Falcon"] mutableCopy];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.cellTitles.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath];

  cell.textLabel.text = self.cellTitles[indexPath.row];

  return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    [self.cellTitles removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  }
}
Keller
  • 17,051
  • 8
  • 55
  • 72
0

You should have add the following delegate method as well:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES - we will be able to delete all rows
    return YES;
}

The whole implementation would be

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return YES - we will be able to delete all rows
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

Based on your updated question, then I would add a edit button on the right top side of the tableview to inform user to delete the selected cell. Check the following threads

Here is also good tutorial, it is worth to check

Community
  • 1
  • 1
casillas
  • 16,351
  • 19
  • 115
  • 215
  • Sorry, I should have mentioned that I do have the canEditRowAtIndexPath method included in my code as well. Please see the edit above. – Adam G Nov 12 '15 at 22:09
  • I'm not having trouble deleting the row. I'm having trouble with didSelectRowAtIndexPath triggering when I'm trying to swipe to show the delete button. – Adam G Nov 12 '15 at 22:15