UIActionSheet to delete a UITableView cell when canceling continues to show the pressed Delete button

When deleting a row in a table view, I want to show an action sheet that needs confirmation in certain situations. When the answer sheet says “Yes” (destructive action), I delete the row of the table, and everything is in order. But when the Cancel button is pressed, I still see the Delete button in the pressed state.

What I do in tableView:commitEditingStyle when deleting:

 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]; 

It is also strange that the showingDeleteConfirmation cell showingDeleteConfirmation is NO , although the Delete button is still visible.

I use a home-made implementation of a block-based action block. There may be a mistake, although I seem to be getting the correct cell in the undo block.

So, how can I reset the “Delete” button to “not pressed” and delete it, and then return the round delete button to a horizontal position, how does it happen when I click somewhere on the screen?

BlockBasedActionSheet.h helper 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 

BlockBasedActionSheet.m implementation 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 
+8
ios iphone uitableview
source share
2 answers

So it works by replacing the first piece of code 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]; 

Rebooting the line does the trick.

+3
source share

From the documentation for tableView:commitEditingStyle : you should not call setEditing: animated: as part of the implementation of this method. If for some reason you should call it after a delay using the performSelector: withObject: afterDelay: function.

I understand that this method is called as soon as the delete button starts to disappear (therefore, showingDeleteConfirmation is NO ), and therefore the material inside your if statement inside your block will not be called. If you set a breakpoint there, am I right?

In any case, you might want to do what is written in the documentation and just return the table to edit mode after a millisecond delay.

edit / update

Created a new project based on navigation in Xcode. Changed this code. Set the table to show 10 rows, each row shows its index path as text. http://dl.dropbox.com/u/2180315/crazycell.zip if you want all this. You will need to delete to remove, I did not select the delete button.

Calling the setEditing a cell object does nothing, even if performSelector... used performSelector... (I don't know why). But you can call setEditing on the tableView , which, as far as I can tell, does what you want to do (returns it to the horizontal delete icons, without the delete button).

If this does not solve your problem, I'm not sure what else can be done: /

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { BlockBasedActionSheet *askSheet = [[BlockBasedActionSheet alloc] initWithTitle:@"Delete entry?" cancelButtonTitle:@"No" destructiveButtonTitle:@"Yes, delete child also records" cancelAction:^{ [self.tableView setEditing:YES animated:YES]; } destructiveAction:^{ NSLog(@"Delete block called"); }]; [askSheet showInView:self.view]; [askSheet release]; } 
0
source share

All Articles