hpique / HPReorderTableView

Drop-in UITableView replacement to reorder cells with long press on any part of the cell
Apache License 2.0
206 stars 38 forks source link

Swipe to Delete #4

Closed Sun3 closed 10 years ago

Sun3 commented 10 years ago

I have a question, now that I am using HPReorderTableView the swipe to show the Delete Button no longer works.

Is there a way to enable it again? I already tried below.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //NSLog(@"FIRST_DELETE");
        NSFetchedResultsController *frc = [self frcFromTV:tableView];
        List *deleteTarget = [frc objectAtIndexPath:indexPath];
        [frc.managedObjectContext deleteObject:deleteTarget];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
    }

Thank you

hpique commented 10 years ago

I confirm this is a bug. The problem is related to the data source forwarding code but at first glance I'm not able to tell what it is.

I don't use the default swipe to delete feature, so it might take me a while to fix this. Please don't hesitate to submit a pull request if you find a fix.

Sun3 commented 10 years ago

When I add below code to the HPReorderTableView class it allows to swipe and the Delete button appears, but once you tab the Delete Button it runs the commitEditingStyle in the HPReorderTableView class instead of the code in my form to run because it now runs in the class instead :)

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
        NSLog(@"Delete");
        ; // Delete.
}
hpique commented 10 years ago

Thanks @Sun3! I'll give it a try later this week.

Sun3 commented 10 years ago

ps: I just re-read my last sentence and it sounds so unclear. Here is what I was trying to say.

Once you place the commitEditingStyle in the HPReorderTableView class, the commitEditingStyle in the xib class does not run because the HPReorderTableView class runs it first.

Thanks.

hpique commented 10 years ago

You can now use HPReorderAndSwipeToDeleteTableView to have both reordering and swipe-to-delete.

The problem was that UITableView enables swipe-to-delete only if the data source implements tableView:commitEditingStyle:forRowAtIndexPath: and checks this at initialisation time. Which in my opinion is a horrible design decision. Since HPReorderTableView uses data source forwarding, it either has to support swipe-to-delete, or not.

Given that swipe-to-delete is disabled by default in UITableView, I choose to keep HPReorderTableView without support for swipe-to-delete and provide a separate class HPReorderAndSwipeToDeleteTableView for those who need. The code documentation has been updated accordingly.

Sun3 commented 10 years ago

That is great, thanks. I tested and it works great...