简体   繁体   English

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

[英]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.但是当按下取消时,我仍然在按下的 state 中看到删除按钮。

What I do in tableView:commitEditingStyle when deleting is this:删除时我在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];

What's also strange is that the cell's property showingDeleteConfirmation is NO , although the Delete button is still visible.同样奇怪的是单元格的showingDeleteConfirmation的属性为NO ,尽管删除按钮仍然可见。

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?那么,如何将删除按钮 state 重置为“未按下”并将其删除,然后将圆形删除按钮恢复为水平状态,就像我在屏幕上点击某处时发生的那样?

Helper class BlockBasedActionSheet.h :助手 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 :实现文件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

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.重新加载行就可以了。

From the documentation for tableView:commitEditingStyle : You should not call setEditing:animated: within an implementation of this method.tableView:commitEditingStyle的文档中您不应该在此方法的实现中调用 setEditing:animated:。 If for some reason you must, invoke it after a delay by using the performSelector:withObject:afterDelay: method.如果出于某种原因必须,请在延迟后使用 performSelector:withObject:afterDelay: 方法调用它。

My understanding is that this method is called once the delete button is beginning to disappear (hence showingDeleteConfirmation is NO ) and so the stuff inside your if statement inside your block won't be called.我的理解是,一旦删除按钮开始消失(因此showingDeleteConfirmationNO ),就会调用此方法,因此不会调用块内 if 语句中的内容。 If you set a breakpoint in there, am I right?如果你在那里设置一个断点,对吗?

In any case, you might want to do as the documentation says and just have the table go back into editing mode after a millisecond delay.在任何情况下,您都可能希望按照文档中的说明进行操作,只需在毫秒延迟后让表 go 回到编辑模式即可。

edit/update编辑/更新

Created a new Navigation based project in Xcode.在 Xcode 中创建了一个新的基于导航的项目。 Changed this code.更改了此代码。 Set the table to show 10 rows with each row displaying its indexpath as the text.将表格设置为显示 10 行,每行将其索引路径显示为文本。 http://dl.dropbox.com/u/2180315/crazycell.zip if you want the whole thing. http://dl.dropbox.com/u/2180315/crazycell.zip如果你想要整件事。 You'll need to swipe to delete, I didn't make a dedicated delete button.您需要滑动才能删除,我没有制作专用的删除按钮。

Calling setEditing on the cell object does nothing, even if performSelector... is used (I don't know why).在单元格 object 上调用setEditing什么也不做,即使使用performSelector... (我不知道为什么)。 But you can call setEditing on the tableView which as far as I can tell does what you want it to do (brings it back to the horizontal delete icons, with no delete button).但是您可以在tableView上调用setEditing ,据我所知,它可以执行您希望它执行的操作(将其带回水平删除图标,没有删除按钮)。

If this doesn't solve your problem then I'm really 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];  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM