用于在取消时删除UITableView单元格的UIActionSheet保持显示按下删除按钮

在表格视图中删除行时,我想在某些情况下显示要求确认的操作表。 当操作表回答“是”(破坏性操作)时,我删除表格行,一切都很好。 但是当按下取消时,我仍然看到处于按下状态的删除按钮。

我在tableView:commitEditingStyle删除时的tableView:commitEditingStyle是这样的:

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

同样奇怪的是,单元格的属性showingDeleteConfirmationNO ,尽管Delete按钮仍然可见。

我正在使用自制的基于块的操作表实现。 也许存在错误,尽管我似乎在取消块中获得了正确的单元格。

那么,如何将删除按钮状态重置为“未按下”并将其删除,然后将圆形删除按钮恢复为水平,就像我点击屏幕上的某个位置时一样?

Helper类BlockBasedActionSheet.h

 @interface BlockBasedActionSheet : UIActionSheet { } @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

 #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 

因此,它可以通过替换第一个代码片段来实现

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

重新加载行就可以了。

tableView:commitEditingStyle的文档tableView:commitEditingStyle你不应该在这个方法的实现中调用setEditing:animated :. 如果由于某种原因你必须在延迟之后使用performSelector:withObject:afterDelay:方法调用它。

我的理解是,一旦删除按钮开始消失(因此showingDeleteConfirmation删除showingDeleteConfirmationNO ),就会调用此方法,因此不会调用块内if语句中的内容。 如果你在那里设置断点,我是对的吗?

在任何情况下,您可能希望按照文档所述进行操作,并且只需在毫秒延迟后让表格返回编辑模式。

编辑/更新

在Xcode中创建了一个新的基于导航的项目。 更改了此代码。 将表设置为显示10行,每行显示其索引路径作为文本。 http://dl.dropbox.com/u/2180315/crazycell.zip如果你想要整件事。 你需要滑动才能删除,我没有制作专门的删除按钮。

在单元对象上调用setEditing什么都不做,即使使用performSelector... (我不知道为什么)。 但你可以在tableView上调用setEditing ,据我所知,你可以告诉你想要它做什么(把它带回到水平删除图标,没有删除按钮)。

如果这不能解决您的问题,那么我真的不确定还能做些什么:/

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