Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate Between UITableView Editing States?

I have been looking at trying to differentiate between editing states in my UITableView.

I need to call a method only when in editing mode after tapping the edit button, so when you get your cell slide in and you see the little circular delete icons but NOT when the user swipes to delete.

Is there anyway I can differentiate between the two?

Thanks.

EDIT:

Solution thanks to Rodrigo

Both each cell and the entire tableview has an 'editing' BOOL value, so I loop through all the cells and if more than one of them is editing then we know the whole table is (the user tapped the edit button), however if only one is editing then we know that the user has swiped a cell, editing that individual one, this lets me deal with each editing state individually!

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];


    int i = 0;
    //When editing loop through cells and hide status image so it doesn't block delete controls. Fade back in when done editing.
    for (customGuestCell *cell in self.tableView.visibleCells) 
    { 
        if (cell.isEditing) {
            i += 1;
        }
    }

    if (i > 1) 
    {
        for (customGuestCell *cell in self.tableView.visibleCells) 
        { 
            if (editing) 
            {
                // loop through the visible cells and animate their imageViews
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.4];
                cell.statusImg.alpha = 0;
                [UIView commitAnimations];
            } 
        }
    }
    else if (!editing) 
    {
        for (customGuestCell *cell in self.tableView.visibleCells) 
        { 
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];
            cell.statusImg.alpha = 1.0;
            [UIView commitAnimations];            
        }
    }
}
like image 360
Josh Kahane Avatar asked Apr 04 '12 12:04

Josh Kahane


2 Answers

Even if this post is quite old, the following might be helpful to others:

If you implement the following delegate messages: - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;

and

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;

These methods will be called when editing a single line. -[UIViewController setEditing:animated:] will then only get called when the user hits the edit button.

like image 150
Michael Ochs Avatar answered Sep 28 '22 17:09

Michael Ochs


There are one strategy, I do not test now, but maybe work.

You can set the UITableView to be in editing mode and test with isEditing function. But the cell have the same isEditing. So you can check if only one cell is in editing state or all the UITableView.

Check if when you set one cell to be in editing state, the UITableView change to editing state at all.

like image 44
Rodrigo Avatar answered Sep 28 '22 17:09

Rodrigo