Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to remove the revealed swipe of a UITableViewCell when using editActionsForRowAtIndexPath:?

I have a tableView in which I am enabling swiping the cell to reveal options - in my case, 'share' and 'delete' options by using the following method:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

when a user swipes on a cell to reveal the options and then taps on delete I put up a UIAlertController with a confirmation. all is well and good if they say delete as i delete the object in my core data database and my NSFetchedResultController takes care of updating the tableView

If the user declines to go through with the delete then the cell remains with the slide effect still revealing the options. I'd like it to go away. I know that [tableView reloadData] or, more efficiently, just reloading the one cell will solve the problem but is there a method call or property on the cell that I've missed that would do the job?

like image 356
SimonTheDiver Avatar asked Jan 28 '15 14:01

SimonTheDiver


2 Answers

If you end editing on the table view here it will animate the cell back to its normal state.

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    [table setEditing:NO animated:YES];
}

If the table was in an editing state where it shows the delete button next to each cell, this will cause all of those delete buttons to go away, which may not be the behavior you want. In this case, you can immediately set the table view back to editing mode. The animation looks fine:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    [table setEditing:NO animated:YES];
    if (yourWasPreviouslyEditingBoolean) {
        [table setEditing:YES animated:YES];
    }
}

Note that you'll need to track this state yourself. Checking the table view's isEditing value will always return true here.

I haven't been able to find away to tell the cell directly to stop editing.

like image 80
Dave Batton Avatar answered Nov 13 '22 13:11

Dave Batton


So, your question is about how to reload one particular cell on a UITableView? Not hard.

The following code will do the trick:-

NSIndexPath * indexpath = [NSIndexPath indexPathForRow:self.deletedTableRow
                                             inSection: self.deletedTableSection];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexpath] 
                      withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
like image 1
Ricky Avatar answered Nov 13 '22 14:11

Ricky