用于在取消时删除UITableView单元格的UIActionSheet始终显示为按下Delete键 [英] UIActionSheet for deleting a UITableView cell on cancel keeps showing pressed Delete button

查看:55
本文介绍了用于在取消时删除UITableView单元格的UIActionSheet始终显示为按下Delete键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

还有一个奇怪的是,尽管删除"按钮仍然可见,但单元格的属性"showingDeleteConfirmation"是"NO".

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

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

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?

助手类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:

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

重新加载行就可以了.

这篇关于用于在取消时删除UITableView单元格的UIActionSheet始终显示为按下Delete键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆