UITableViewCell默认为长按出现菜单,现需要点击出现菜单,用在评论中,点击回复,复制 等功能
实现点击自定义UITableViewCell时展示一个自定义菜单的功能。
//全局初始化菜单-(void)initMenu{ UIMenuController *menuController = [UIMenuController sharedMenuController]; [menuController setMenuVisible:YES animated:YES]; [menuController setMenuItems:@[ [[UIMenuItem alloc] initWithTitle:@"回复" action:NSSelectorFromString(@"replyObject:")], [[UIMenuItem alloc] initWithTitle:@"复制" action:NSSelectorFromString(@"copyText:")], [[UIMenuItem alloc] initWithTitle:@"举报" action:NSSelectorFromString(@"reportObject:")] ]];}//// UITableViewCell.m- (void)addTap{ //UITableViewCell中添加 [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu)]];}#pragma mark - tap- (void)showMenu{ if(self.isFirstResponder){ [self resignFirstResponder]; }else{ [self becomeFirstResponder]; } NSLog(@"showMenu:------%@", @(self.isFirstResponder)); UIMenuController *menuController = [UIMenuController sharedMenuController]; [menuController setMenuVisible:YES animated:YES]; [menuController setTargetRect:_contentLabel.frame inView:self];}#pragma mark - FirstResponder()- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender{// return _canPerformAction(self, action); return action==@selector(replyObject:)|| action==@selector(reportObject:)|| action==@selector(copyText:);}- (BOOL)canBecomeFirstResponder{ return YES;}- (void)copyText:(id)sender{ UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; [pasteBoard setString:_contentLabel.text]; [MBProgressHUD showText:@"已复制"];}- (void)reportObject:(id)sender{ _reportObject(self);}- (void)replyObject:(id)sender{ _replyObject(self);}
UITableViewCell点击的自定义菜单 :