給TableViewCell添加自定義定制(注意是定制 )UIMenuController的兩種方法

一、使用UITableViewDelegate方法無需子類化
前提:我們假設(shè)一個(gè)控制器中有一個(gè)tableView,并完成了tableView的基本設(shè)置。
0.在viewDidLoad中:給menu添加一個(gè)舉報(bào)的Item

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"舉報(bào)" action:@selector(report:)];

UIMenuController *menu=[UIMenuController sharedMenuController];

[menu setMenuItems: @[testMenuItem]];

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

1.是否顯示Menu

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

  return YES;

}

2.是否該顯示系統(tǒng)菜單選項(xiàng)并調(diào)(就是到底顯示系統(tǒng)的哪幾項(xiàng))

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
//顯示一個(gè)系統(tǒng)的,一個(gè)自定義的
 return (action == @selector(copy:) || action == @selector(report:));

}

3.根據(jù)action來判斷具體要什么執(zhí)行動(dòng)作

-(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...");

     }

}

二、使用長(zhǎng)按手勢(shì)加子類化(大多數(shù)用到cell都會(huì)把它子類化的)
0.在cell子類化初始化方法中或者在數(shù)據(jù)源方法返回cell的時(shí)候都可以加上手勢(shì)

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

[cell addGestureRecognizer:longPress];

1.實(shí)現(xiàn)長(zhǎng)按方法

-(void)longPress:(UILongPressGestureRecognizer *)recognizer

{

    if (recognizer.state == UIGestureRecognizerStateBegan) {

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

    [cell becomeFirstResponder];

    UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"舉報(bào)"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子類化實(shí)現(xiàn)兩個(gè)方法

//只有成為第一響應(yīng)者時(shí)menu才會(huì)彈出
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
//是否可以接收某些菜單的某些交互操作
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:) || action == @selector(report:));
}
//實(shí)現(xiàn)這兩個(gè)方法來執(zhí)行具體操作
-(void)copy:(id)sender{
    [UIPasteboard generalPasteboard].string=self.comment.Content;
}
-(void)report:(id)sender{
    NSLog(@"舉報(bào)啦");
}

總結(jié):第一種用起來比較簡(jiǎn)便,適合沒有特殊要求的UIMenuController,由于一般使用cell都需要子類化,而且第二種更容易控制菜單控制器具體顯示的位置和方向以及一些其他屬性,比較靈活,看大家喜好了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 昨天是我的生日,平時(shí)消費(fèi)的美容院的姐妹們費(fèi)盡心思,給我弄了一個(gè)溫馨而感人的生日?qǐng)雒妫业难蹨I都快沒有忍住。 我是有...
    c7690f3ff548閱讀 634評(píng)論 0 0
  • 群山被雕刻成精品 每一座都神態(tài)各異 那是蕓蕓眾生 三千世界的縮影 每一個(gè)背后都有千年的歷史 我踩著白霧而至 在朦朧...
    栗子顏閱讀 706評(píng)論 0 4
  • 00 預(yù)測(cè),是對(duì)尚未發(fā)生的事物進(jìn)行預(yù)先估計(jì),并推測(cè)事物未來的發(fā)展情況。有人說,這帶有濃烈的迷信色彩。然而,萬事萬物...
    楊喜愛閱讀 1,476評(píng)論 7 39
  • 喜歡優(yōu)美的文字,喜歡生動(dòng)的電影情節(jié),喜歡行走飄逸的書法,喜歡一切美麗的事物。自認(rèn)為作為一個(gè)男人,不能被作品里的情節(jié)...
    頑強(qiáng)的potato閱讀 343評(píng)論 0 0

友情鏈接更多精彩內(nèi)容