给TableViewCell添加自定义定制(注意是定制 )UIMenuController的两种方法

一、使用UITableViewDelegate方法无需子类化
前提:我们假设一个控制器中有一个tableView,并完成了tableView的基本设置。
0.在viewDidLoad中:给menu添加一个举报的Item

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(report:)];

UIMenuController *menu=[UIMenuController sharedMenuController];

[menu setMenuItems: @[testMenuItem]];

[menu update];//必须要更新

1.是否显示Menu

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {

  return YES;

}

2.是否该显示系统菜单选项并调(就是到底显示系统的哪几项)

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
//显示一个系统的,一个自定义的
 return (action == @selector(copy:) || action == @selector(report:));

}

3.根据action来判断具体要什么执行动作

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

    if (action == @selector(copy:)) {

     NSLog(@"copy..."); 

     }

    else if(action == @selector(report:)){

     NSLog(@"report...");

     }

}

二、使用长按手势加子类化(大多数用到cell都会把它子类化的)
0.在cell子类化初始化方法中或者在数据源方法返回cell的时候都可以加上手势

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

[cell addGestureRecognizer:longPress];

1.实现长按方法

-(void)longPress:(UILongPressGestureRecognizer *)recognizer

{

    if (recognizer.state == UIGestureRecognizerStateBegan) {

    YDSonCommentCell *cell = (YDSonCommentCell *)recognizer.view;

    [cell becomeFirstResponder];

    UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"举报"action:@selector(report:)];
    UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"拷贝"action:@selector(copy:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];

    [menu setMenuItems:[NSArray arrayWithObjects:report,copy, nil]];

    [menu setTargetRect:cell.frame inView:cell.superview];

    [menu setMenuVisible:YES animated:YES];

   }
}

2.cell子类化实现两个方法

//只有成为第一响应者时menu才会弹出
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
//是否可以接收某些菜单的某些交互操作
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:) || action == @selector(report:));
}
//实现这两个方法来执行具体操作
-(void)copy:(id)sender{
    [UIPasteboard generalPasteboard].string=self.comment.Content;
}
-(void)report:(id)sender{
    NSLog(@"举报啦");
}

总结:第一种用起来比较简便,适合没有特殊要求的UIMenuController,由于一般使用cell都需要子类化,而且第二种更容易控制菜单控制器具体显示的位置和方向以及一些其他属性,比较灵活,看大家喜好了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 昨天是我的生日,平时消费的美容院的姐妹们费尽心思,给我弄了一个温馨而感人的生日场面,我的眼泪都快没有忍住。 我是有...
    c7690f3ff548阅读 3,663评论 0 0
  • 群山被雕刻成精品 每一座都神态各异 那是芸芸众生 三千世界的缩影 每一个背后都有千年的历史 我踩着白雾而至 在朦胧...
    栗子颜阅读 3,873评论 0 4
  • 00 预测,是对尚未发生的事物进行预先估计,并推测事物未来的发展情况。有人说,这带有浓烈的迷信色彩。然而,万事万物...
    杨喜爱阅读 5,261评论 7 39
  • 喜欢优美的文字,喜欢生动的电影情节,喜欢行走飘逸的书法,喜欢一切美丽的事物。自认为作为一个男人,不能被作品里的情节...
    顽强的potato阅读 2,299评论 0 0

友情链接更多精彩内容