testuser
testuser

Reputation: 982

UITableView (IOS) trying to delete but never get the delete button to show up

I know there's a lot of questions like this one out there, but I've followed a couple of them and none of them could lead me to the answer.

Probably it's just something stupid and obvious I'm missing but I can't get it...

So what I'm doing is, I have a normal UITableViewController. In the table I want to display all files the user downloaded via my app and give him the opportunity to delete the files as well. Everything works fine - I can list up the files, and when clicked another view opens to display the file.

But I never can get the row to delete something. I swiped all possible ways but I never even get the delete button! I have implemented the canEditRowAtIndexPath function to make it return YES (although it shouldn't be necessary, I've made tons of apps where I deleted rows and never needed the function):

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

My actual delete method is implemented like this:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSError *error = [[NSError alloc] init];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *fileName = [NSString stringWithFormat:@"%@/%@", directory, [files objectAtIndex:indexPath.row]];
        BOOL success = false;

        if ([fileManager isDeletableFileAtPath:fileName]) {
            success = [fileManager removeItemAtPath:fileName error:&error];
        }

        if (success) {
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [files removeObjectAtIndex:indexPath.row];
        } else {
            [Functions msg:NSLocalizedString(@"fileCantBeDeleted", @"") title:NSLocalizedString(@"fileError", @"") delegate:nil];
            if (error) {
                NSLog(@"Error: %@", [error localizedDescription]);
            }
        }
    }
}

To be complete, I can give you the code of cellForRowAtIndexpath as well:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [files objectAtIndex:indexPath.row];
    return cell;
}

The NSArray "files" has been filled before, in the viewDidLoad method like this:

directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:nil];

Both directory and files are properties of my class. As said, the whole class is just implementing a UITableViewController. I really can't see why it wouldn't let me delete a row, but probably it's something really stupid... I just can't see it.

EDIT

Found the bug.

This particular TableView doesn't itself implement any methods of the PKRevealController library, but it was apparently set as the frontViewController - so there happened to be a gesture on there that I didn't know of. The controller also was not intended to be used inside a PKRevealController... I just added it in a wrong way, so I wasn't aware of that either! That's also why I forgot to include it in my question.

I found the solution here: https://github.com/pkluz/PKRevealController/issues/123 (for if anyone else might ever run into this problem).

Thanks for helping me anyway!

EDIT

Another possible solution I just learned about if you want to use a tableView and still keep deleting things while using a PKRevealController (without starting to get strange with UIGestureRecognizers): You can just add an edit button to trigger the view into editing mode.

self.navigationItem.rightBarButtonItem = self.editButtonItem;

This works perfectly for me.

Upvotes: 2

Views: 1140

Answers (2)

Prasanna
Prasanna

Reputation: 955

Make Your tableview editable

You need to create an object of your table view ....

in .h file of the ViewController.

UITableView *table;

in .m file of the ViewController you need to set the editing mode also of the tableview...

- (void)viewDidLoad{

table.editing=YES;

[super viewDidLoad];

}

Hope this will help you ......

Upvotes: 3

Michael Dautermann
Michael Dautermann

Reputation: 89509

You need to add the "editingStyleForRowAtIndexPath" method into your delegate:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return( UITableViewCellEditingStyleDelete );
}

Upvotes: 0

Related Questions