Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect the difference between a swipe-to-delete button and a delete button made in Edit mode?

I currently have a UITableView set up to enable swipe-to-delete (a swipe shows the Delete button). I also have an Edit button which, when pushed, slides in the red deletion circles for each cell. Tapping one of those circles causes the Delete button to show as if I'd swiped the cell.

I'd like to detect whether the Delete button was created via one of the red circles (i.e. in Edit mode) or by directly swiping on the cell (reason being I'd like the user to confirm if they didn't come in via Edit mode, since they can potentially lose a lot of data if they mistapped it).

I've tried the isEditing property of the tableView, but that's YES however I got to the delete button. Is there a more nuanced way to detect this?

Thanks in advance!

like image 519
Luke Avatar asked Dec 20 '22 21:12

Luke


1 Answers

The much simpler approach is to use the UITableView delegate support to know when you are about to enter single row editing. The tableView:willBeginEditingRowAtIndexPath: delegate call occurs BEFORE calling setEditing:animated: which allows you to avoid any normal edit setup such as creating an insert row, etc. The tableView:willEndEditingRowAtIndexPath: is called AFTER setEditing:animated.

So the code is as simple as:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.singleRowDeleteMode = YES;
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.singleRowDeleteMode = NO;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (self.singleRowDeleteMode) { return; }
    // Continue setup of normal edit mode
}
like image 89
dlemex Avatar answered Dec 22 '22 11:12

dlemex