首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在UITableViewCell中显示UIView上的longpress菜单

在UITableViewCell中显示UIView上的longpress菜单
EN

Stack Overflow用户
提问于 2018-06-02 04:40:05
回答 1查看 626关注 0票数 0

在IOS应用程序中,当我长按位于UITableViewCell中的UIView时,我想显示一个UIMenuController。

代码语言:javascript
复制
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    myTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell==nil)
    {
        cell=(myTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    myView=<initMyView>
    [cell.contentView addSubview:myView];
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCapturedOnView:)];
    longTap.minimumPressDuration=0.5f;
    [myView addGestureRecognizer:longTap];

    return cell;
}

- (void)longTapGestureCapturedOnView:(UITapGestureRecognizer *)gesture
{
    items=[[NSMutableArray alloc]init];
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        [items addObject:[[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(myCopy:)]];
        [items addObject:[[UIMenuItem alloc] initWithTitle:@"Custom" action:@selector(myCustom:)]];
    }

    [[UIMenuController sharedMenuController] setMenuItems:items];
    [[UIMenuController sharedMenuController] update];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    BOOL result = NO;
    if(@selector(myCopy:) == action ||@selector(myCustom:) == action ) {
        result = YES;
    }
    return result;
}

调用longTapGestureCapturedOnView,以及在两种情况下返回YES的canPerformAction,但屏幕上没有出现菜单项。我能做错什么呢?

EN

回答 1

Stack Overflow用户

发布于 2018-06-02 05:16:37

您还需要

代码语言:javascript
复制
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
   return YES ;
}

//

此外,在cellForRowAt中添加手势也是不正确的,因为它是在scroll & cellReusing上调用的,所以请将其添加到UITableViewCell自定义类的awakeFormNib方法中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50650727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档