IOS学习笔记36——UIPasteboard/UIMenuController(二)

好,上一篇咱们讲到了在UILabel上生成一个剪贴板,这篇咱写写一写在UITableView上实用剪贴板的两种方法;

一:在自定义cell里面使用

 其实,在cell的使用跟在label里面的用法基本一样

1.放方法:

- (BOOL)canBecomeFirstResponder{

    return YES;

}

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

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

        return NO;

    }

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

        return YES;

    }

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

        return NO;

    }

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

        return NO;

    }

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

        return NO;

    }

    else{

        return [super canPerformAction:action withSender:sender];

    }

}

然后呢,当然就是 实现copy方法拉。。

- (void)copy:(id)sender {

    NSLog(@"copy");

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

   [pasteboard setString:[[self textLabel]text]];

  }

不过还有最重要的一步,上此方法,它的意思是用来激发copy菜单哦。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {

    [[self delegate] performSelector:@selector(showMenu:)

      withObject:self afterDelay:0.9f];

[super setHighlighted:highlighted animated:animated];

 }

至于生成菜单,大家应该已经经过上一篇的阅读,已经很熟悉了,看代码:

//显示菜单

- (void)showMenu:(id)cell {

    if ([cell isHighlighted]) {

         [cell becomeFirstResponder];

        UIMenuController * menu = [UIMenuController sharedMenuController];

        [menu setTargetRect: [cell frame] inView: [self view]];

        [menu setMenuVisible: YES animated: YES];

    }

}

OK,剩下的至于如何实用自定义的cell,大家应该已经很熟练了吧,我就不在一一介绍了,自此,第一种使用剪贴板的方法介绍完毕

二:第二种就更简单了,因为(__MAC_NA,__IPHONE_5_0)以后已经放出了3个方法,非常好用,我直接就上方法拉。

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

    

    return YES;

}


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

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

        return YES;

    }

    

    return NO;

}


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

    

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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

        [UIPasteboard generalPasteboard].string = cell.textLabel.text;

    }

    

}

有上面三个方法,我想,你肯定能轻松实现cell里面剪贴板的使用了。。

好,第二篇介绍完毕,继续上重点,自定义剪贴板-----见下版




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值