Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActionSheet for deleting a UITableView cell on cancel keeps showing pressed Delete button

When deleting a row in a table view, I want to show an action sheet asking for confirmation in certain situations. When the action sheet is answered with "Yes" (the destructive action), I delete the table row and everything is fine. But when Cancel is pressed, I still see the Delete button in a pressed state.

What I do in tableView:commitEditingStyle when deleting is this:

BlockBasedActionSheet *askSheet =
    [[BlockBasedActionSheet alloc] 
          initWithTitle:@"Delete entry?"
      cancelButtonTitle:@"No"
 destructiveButtonTitle:@"Yes, delete child also records"
           cancelAction:^{
               UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
               if (cell.showingDeleteConfirmation) {
                   cell.editing = NO;
                   cell.editingAccessoryView = nil;
                   cell.editing = YES;
               }
           }
  destructiveAction:deleteBlock];
[askSheet showInView:self.view];
[askSheet release];

What's also strange is that the cell's property showingDeleteConfirmation is NO, although the Delete button is still visible.

I am using a self-made block-based action sheet implementation. Maybe there lies the error, although I seem to get the correct cell in the cancel block.

So, how can I reset the Delete button state back to "unpressed" and remove it, and turn the round delete button back to horizontal, like it happens when I tap somewhere on the screen?

Helper class BlockBasedActionSheet.h:

@interface BlockBasedActionSheet : UIActionSheet<UIActionSheetDelegate> {
}

@property (copy) void (^cancelBlock)();
@property (copy) void (^destructiveBlock)();

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle cancelAction:(void (^)())cancelBlock destructiveAction:(void (^)())destructiveBlock;

@end

Implementation file BlockBasedActionSheet.m:

#import "BlockBasedActionSheet.h"

@implementation BlockBasedActionSheet
@synthesize cancelBlock = _cancelBlock, destructiveBlock = _destructiveBlock;

- (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle cancelAction:(void (^)())cancelBlock destructiveAction:(void (^)())destructiveBlock
{
    self = [super initWithTitle:title delegate:self cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles: nil];
    if (self) {
        _cancelBlock = Block_copy(cancelBlock);
        _destructiveBlock = Block_copy(destructiveBlock);
    }
    return self;
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSAssert(actionSheet == self, @"Wrong Action Sheet passed");
    if (buttonIndex == [self cancelButtonIndex]) {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
    } else {
        if (self.destructiveBlock) {
            self.destructiveBlock();
        }
    }
}

@end
like image 261
Björn Landmesser Avatar asked Jun 08 '11 21:06

Björn Landmesser


1 Answers

So, it works by replacing the first code snippet with this

BlockBasedActionSheet *askSheet =
    [[BlockBasedActionSheet alloc] 
          initWithTitle:@"Delete entry?"
      cancelButtonTitle:@"No"
 destructiveButtonTitle:@"Yes, delete also child records"
           cancelAction:^{
           // this resets the delete button
           [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                            withRowAnimation:UITableViewRowAnimationFade];
           }
  destructiveAction:deleteBlock];
[askSheet showInView:self.view];
[askSheet release];

Reloading the row does the trick.

like image 51
Björn Landmesser Avatar answered Nov 15 '22 05:11

Björn Landmesser