以下是实现点击自定义UITableViewCell时展示一个自定义菜单实现回复、举报和复制文字的功能。
- 1.在自定义UITableViewCell中实现以下方法:
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return action==@selector(replyAction:)||
action==@selector(reportAction:)||
action==@selector(copyAction:);
}
- 2.实现三个自定义菜单项点击事件:
-(void)replyAction:(id)sender
{
//走代理方法
}
-(void)reportAction:(id)sender
{
//走代理方法
}
-(void)copyAction:(id)sender
{
UIPasteboard *pboard = [UIPasteboard generalPasteboard];
pboard.string = _comment.content;
}
- 3.在自定义UITableViewCell中添加单击手势:
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(showMenu)];
[self addGestureRecognizer:tapGesture];
- 4.实现点击展示菜单方法:
-(void)showMenu
{
if(self.isFirstResponder){
[self resignFirstResponder];
self.avatarBtn.userInteractionEnabled = YES;
}else{
[self becomeFirstResponder];
self.avatarBtn.userInteractionEnabled = NO;
}
UIMenuItem *replyItem = [[UIMenuItem alloc]initWithTitle:@"回复" action:@selector(replyAction:)];
UIMenuItem *reportItem = [[UIMenuItem alloc]initWithTitle:@"举报" action:@selector(reportAction:)];
UIMenuItem *copyItem = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyAction:)];
UIMenuController *menuController = [UIMenuController sharedMenuController];
menuController.menuItems = @[replyItem,reportItem,copyItem];
[menuController setTargetRect:self.frame inView:self.superview];
[menuController setMenuVisible:YES animated:YES];
}
- 5.效果图
14556931660733.jpg
网友评论