在UICollectionView中更改菜单的标签标题

时间:2013-09-27 11:16:25

标签: iphone ios objective-c cocoa-touch uicollectionview

在点击uicollectionviewcell时,我想使用与菜单栏相同的外观和感觉:

enter image description here

但是,我想写一下移动

而不是剪切

有可能吗?我看到了一些答案 - 但他们都要求实现我自己的UIActionSheet - 但我希望它看起来像一个菜单而不是一个行动表

有可能吗?

2 个答案:

答案 0 :(得分:1)

    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
    longPressGesture.minimumPressDuration=0.4;
    [longPressGesture setDelegate:self];
    [self.collectionCell addGestureRecognizer:longPressGesture];

- (void) showMenu:(UILongPressGestureRecognizer *)gestureRecognizer
{

    if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {

          UIMenuController *menuController = [UIMenuController sharedMenuController];
            UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Cut" action:@selector(copyAction:)];
            UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"Paste" action:@selector(pasteAction)];
            CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];

            [menuController setMenuItems:[NSArray arrayWithObjects: menuItem1, menuItem1,nil]];
            [menuController setTargetRect:CGRectMake(location.x, location.y, 0, 0) inView:[gestureRecognizer view]];
            [menuController setMenuVisible:YES animated:YES];

               [self becomeFirstResponder];


    }
}

答案 1 :(得分:0)

如果有其他人需要:

这是correct answer from @Nilz11

添加到ViewDidLoad

   UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editMe:)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Move" action:@selector(moveMe:)];
    UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"DeleteMe" action:@selector(deletePlate:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:menuItem,menuItem2,menuItem3, nil]];

添加委托方法

-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    if (action == @selector(editMe:)  || action == @selector(moveMe:) || action == @selector(deleteMe:))
        return YES;
    return NO;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

将方法添加到自定义UICollectionViewCell 。例如:

-(void)editMe:(UIMenuController *)menuController{
}