长按tableViewCell弹出菜单栏粘贴板

效果图
长按tableViewCell弹出菜单.png
关键代码

吐槽:今天在写这个界面的使用用的是QBPopupMenu这个第三方库,这个点赞量超过了1k,用着也是挺方便的,但是在tableViewCell上面使用的时候出现了一个bug,因为cell复用的原因,在网上找了好久,发现都是抄袭的一两篇比较早的博文,经过仔细研究终于把bug解决了。

bug现象

1、弹出的菜单视图没有出现在我想要的地方
2、长按点击时,有时候不出现菜单视图
3、如果把[self.popupMenu showInView:self targetRect:self.bounds animated:YES];这句代码写在自定制cell里面,没有出现弹出视图

注意鼠标


bug.gif
bug解决代码
[self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];

tableViewCell出现粘贴板的方式

  • 1、 tableViewCell的代理事件

    • - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  • - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender

  • - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender

  • 2、使用UIMenuController

  • 3、使用QBPopupMenu

1、 tableViewCell的代理事件
// 允许长按菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 允许每一个Action
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
{
    // 可以支持所有Action,也可以只支持其中一种或者两种Action
    if (action == @selector(copy:) || action == @selector(paste:)) { // 支持复制和黏贴
        return YES;
    }
    return NO;
}
// 对一个给定的行告诉代表执行复制或黏贴操作内容
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath*)indexPath withSender:(id)sender
{
    if (action == @selector(copy:)) { 
        NSLog(@"复制");
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; // 黏贴板
        [pasteBoard setString:cell.textLabel.text];
    } else if (action == @selector(paste:)) {
        NSLog(@"黏贴");
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        NSLog(@"%@",pasteBoard.string);
    } else if (action == @selector(cut:)) {
        NSLog(@"剪切");
    }
}

效果图

屏幕快照 2017-05-24 下午6.45.40.png

如果想要自定制上面的文字,表示不太清楚,知道的同学可以吱一声。如果想要自定制可以使用UIMenuController

2、使用UIMenuController

UIMenuController,系统默认支持UITextField、UITextView、UIWebView控件的UIMenuController相关操作。当我们长按一段文字或者图片的时候会弹出一个菜单,我们通过这个菜单可以实现文字等的复制、剪切、删除以及各种操作。

UIMenuController相关方法

创建一个UIMenuController对象

+ (UIMenuController *)sharedMenuController

显示或者隐藏菜单,注意 在显示menu之前,一点要确定为menu设置与其相关的显示位置

@property(nonatomic,getter=isMenuVisible) BOOL menuVisible;        // default is NO
//是否通过动画进行设置显示、隐藏
- (void)setMenuVisible:(BOOL)menuVisible animated:(BOOL)animated;

设置menu显示的位置

/**
 *  设置menu显示的位置信息
 *
 *  @param targetRect menu需要显示的矩形区域
 *  @param targetView targetRect会以targetView的左上角为坐标原点进行显示
 */
- (void)setTargetRect:(CGRect)targetRect inView:(UIView *)targetView;
注意

targetRect一旦设定以后,矩形范围不会跟随view的移动而移动,如果view移动,必须相应的更新targetRect 。比如tableView 点击cell出现menu,当按住对应的cell,拖动tableView滚动时,menu不会随着对应的cell一起滚动---见示例代码2
targetRect通常设置为需要弹出menu控件的bounds,targetView设置为对应的控件本身

自定义menuItem

@property(nonatomic, copy) NSArray <UIMenuItem *> *menuItems

@interface UIMenuItem : NSObject 
//创建UIMenuItem对象
- (instancetype)initWithTitle:(NSString *)title action:(SEL)action ;
@property(nonatomic,copy) NSString *title;
@property(nonatomic)      SEL       action;

数据类型:编辑菜单箭头指向view的位置 。默认取决于view在界面的位置

typedef enum {
 UIMenuControllerArrowDefault,
 UIMenuControllerArrowUp,
 UIMenuControllerArrowDown,
 UIMenuControllerArrowLeft,
 UIMenuControllerArrowRight,
} UIMenuControllerArrowDirection;
自定义控件的UIMenuController

一般步骤:

  • 设置控件成为第一响应者
  • 创建UIMenuControler
  • 创建UIMenuItem(如果需要自定义item)

在自定制cell里面

- (void)addGesture{
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressCellHandle:)];
   
    [self addGestureRecognizer:longPressGesture];
}

-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture

{
    
    [self becomeFirstResponder];
    
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    
    UIMenuItem *checkItem = [[UIMenuItem alloc] initWithTitle:@"审批" action:@selector(menuBtnPressed:)];
    _checkItem = checkItem;
    UIMenuItem *rejectItem = [[UIMenuItem alloc] initWithTitle:@"驳回" action:@selector(menuBtnPressed:)];
    _rejectItem = rejectItem;
    UIMenuItem *postponeItem = [[UIMenuItem alloc] initWithTitle:@"延期" action:@selector(menuBtnPressed:)];
    _postponeItem = postponeItem;
    UIMenuItem *moreItem = [[UIMenuItem alloc] initWithTitle:@"更多..." action:@selector(menuBtnPressed:)];
    _moreItem = moreItem;
    menuController.menuItems = @[checkItem,rejectItem,postponeItem,moreItem];
    
    [menuController setTargetRect:gesture.view.frame inView:gesture.view.superview];
    
    [menuController setMenuVisible:YES animated:YES];
    
    [UIMenuController sharedMenuController].menuItems=nil;
    
}


-(BOOL)canBecomeFirstResponder

{
    return YES;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender

{
   
    if (action == @selector(menuBtnPressed:)) {
        
        return YES;
        
    }
    
    return NO;
    
}

#pragma mark ---- 菜单按钮执行事件 ----
-(void)menuBtnPressed:(UIMenuItem *)menuItem

{
    
}
防止拖动tableView时产生的BUG

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
}

想要了解更加详细的内容,可以参考UIMenuController的使用简介

3、使用QBPopupMenu


- (void)addGesture{
    [self becomeFirstResponder];
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressCellHandle:)];
   
    [self addGestureRecognizer:longPressGesture];
    
    QBPopupMenuItem *item = [QBPopupMenuItem itemWithTitle:@"Hello" target:self action:@selector(action)];
    QBPopupMenuItem *item2 = [QBPopupMenuItem itemWithTitle:@"Cut" target:self action:@selector(action)];
    QBPopupMenuItem *item3 = [QBPopupMenuItem itemWithTitle:@"Copy" target:self action:@selector(action)];
    QBPopupMenuItem *item4 = [QBPopupMenuItem itemWithTitle:@"Delete" target:self action:@selector(action)];
    QBPopupMenuItem *item5 = [QBPopupMenuItem itemWithImage:[UIImage imageNamed:@"clip"] target:self action:@selector(action)];
    QBPopupMenuItem *item6 = [QBPopupMenuItem itemWithTitle:@"Delete" image:[UIImage imageNamed:@"trash"] target:self action:@selector(action)];
    NSArray *items = @[item, item2, item3, item4, item5, item6];
    
    QBPopupMenu *popupMenu = [[QBPopupMenu alloc] initWithItems:items];
    popupMenu.highlightedColor = [[UIColor colorWithRed:0 green:0.478 blue:1.0 alpha:1.0] colorWithAlphaComponent:0.8];
    self.popupMenu = popupMenu;
}

-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture

{
    [self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];
}

当时我写的时候主要是这句代码没有理解好

-(void)longPressCellHandle:(UILongPressGestureRecognizer *)gesture

{
   [self.popupMenu showInView:gesture.view.superview targetRect:gesture.view.frame animated:YES];
}
简单的总结一下,希望能过帮助跟我遇到一样问题的小朋友
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,458评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,030评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,879评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,278评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,296评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,019评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,633评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,541评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,068评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,181评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,318评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,991评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,670评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,183评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,302评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,655评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,327评论 2 358

推荐阅读更多精彩内容