当我点击ios中的视图时,如何关闭tableviewcell中的动作表

时间:2016-04-29 11:47:04

标签: ios objective-c uitableview uilabel uiactionsheet

这是我的Code.Am对Xcode来说很新。当我点击tableview单元格获取UIActionSheet.To解雇UIActionSheet我使用了UITapGestureRecognizer。它不适合我。

    - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
    UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:2];
    NSLog(@"cell text label =%@",lbl.text);
    _StoreNameobj = lbl.text;


    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Store Maintenance Action"
                                                             delegate:self
                                                    cancelButtonTitle:cancelTitle
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Show Store Details",@"Navigate to Store",@"Edit Store Nickname",@"Delete Store", nil];


    actionSheet.userInteractionEnabled = YES;

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)];
    [lbl addGestureRecognizer:tapGesture];
    [cell.contentView addSubview:lbl];
}

    - (void)tapOut{
    //dismiss the action sheet here
    [actionSheet removeFromSuperview];
}

任何帮助都是值得赞赏的。谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您无需手动删除操作表。默认情况下,动作表会在单击时退出。我认为没有必要采取标签手势。

显示动作表,例如

[actionSheet showInView:self.view];

所以当你按下任何按钮时它会自动消失

答案 1 :(得分:0)

要在UITableViewCell上查看UIActionSheet,请应用此代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Your Title:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
                        @"button title1", nil];

  [popup show];
}

添加TapGesture

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UITapGestureRecognizer *tapp = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(HideActionSheet:)];

        [tapp setNumberOfTapsRequired:1];
        tapp.cancelsTouchesInView = NO;
        [self.view.window tapp];
        [tapp release];
}

HideActionSheet代码:

- (void) HideActionSheet:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
     { 
       CGPoint location = [sender locationInView:nil];  
        if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil]) 
        {
          [self.view.window removeGestureRecognizer:sender];
          [self dismissModalViewControllerAnimated:YES];
        }
     }
}