Maneesh Sharma Maneesh Sharma - 11 days ago 7
Objective-C Question

Set action when table view cell is swiped back in a UITableIview

I am using a tableview to list files . And it works perfectly. However , when I swipe left for deleting , the tableview goes into editing mode and my edit button gets overwritten by a cancel button. that is perfect as well . But when I swipe back without deleting , the button should go back to edit , which doesnt happen. Is there any delegate that gets called when a tableview cell is swiped back ? this is the related code

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[[NSUserDefaults standardUserDefaults]objectForKey:DOCUMENTDELETE]isEqualToString:ENABLED]) {

return YES;
} else {
return NO;
}
}

-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView setEditing:NO];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.tableView.editing) {
return UITableViewCellEditingStyleDelete;
}
else {
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = self.cancelButton;
return UITableViewCellEditingStyleDelete;
}
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

self.navigationItem.rightBarButtonItem = nil ;
self.navigationItem.rightBarButtonItem = self.editButton;
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSString *cellText;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
cellText = selectedCell.textLabel.text;
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:@"/Inbox/"];
NSString *allFilesPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSString *theactualFilePath = [allFilesPath stringByAppendingPathComponent:cellText];

NSError *error;

_success = [[NSFileManager defaultManager] removeItemAtPath:theactualFilePath error:&error];
[self.tableView setEditing:NO];
if ([_filePathsArray count] == 0) {
[[NSUserDefaults standardUserDefaults] setObject:DISABLED forKey:NOMATCHFOUND];
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:ALLFILESDELETED];
[[NSUserDefaults standardUserDefaults]synchronize];

}
if (_success) {
}
else
{
NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
}
}

[self.tableView reloadData];

}


this is the code in ViewDidLoad :

if ([[[NSUserDefaults standardUserDefaults] objectForKey:DOCUMENTDELETE] isEqualToString:ENABLED]) {
if (self.tableView.editing){
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = self.cancelButton;
}else {
self.navigationItem.rightBarButtonItem = nil;
[self.navigationItem setRightBarButtonItem:self.editButton];
}} else {
self.navigationItem.rightBarButtonItem = nil;
}


Thanks in advance

Answer

It sounds like you simply need to implement the delegate method

tableView:didEndEditingRowAtIndexPath:

which is where you would catch the table view cell being un-set from editing state.