在UITableView上实用剪贴板有两种方法:
一、在tableView的代理方法中直接有三个有关剪贴板的方法。
//某行是否允许show菜单
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//show菜单中能使用的功能
-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(cut:)){
return YES;
} else if(action == @selector(copy:)){
return YES;
}
else if(action == @selector(paste:)){
return YES;
}
else if(action == @selector(select:)){
return NO;
}
else if(action == @selector(selectAll:)){
return YES;
} else {
return [super canPerformAction:action withSender:sender];
}
}
//点击菜单中调用
-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action ==@selector(copy:)) {
//to do ,然后刷新tableView
}
if (action ==@selector(cut:)) {
}
if (action ==@selector(paste:)) {
}
}
效果棒棒嗒。
二、自定义tableViewCell中的粘贴板
在tableViewCell中实现
-(BOOL) canPerformAction:(SEL)action withSender:(id)sender { return (action == @selector(copy:) || action == @selector(test:));}-(BOOL)canBecomeFirstResponder { return YES;}/// this methods will be called for the cell menu items-(void) test: (id) sender {}-(void) copy:(id)sender {}
不过还有最重要的一步,激发copy菜单,有多种方式。
1.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[[self delegate] performSelector:@selector(showMenu:)
withObject:self afterDelay:0.9f];
[super setHighlighted:highlighted animated:animated];
}
//delegate 中
- (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];
}
}
2. 设置一个手势用来激发 ps:为了让菜单显示,目标视图必须在 responder 链中,很多 UIKit 视图默认并无法成为一个 responder ,因此你需要之类这些视图重载 canBecomeFirstResponder 方法范围 YES
在cell中加一个手势:和delegate原理是一样的。
// CGRect popoverRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];
//
// FXCell.m
// FeixunUniversal
//
// Created by zpz on 15/10/11.
// Copyright (c) 2015年 zpz. All rights reserved.
//
#import "FXCell.h"
@implementation FXCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPressGesture];
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor yellowColor];
}
return self;
}
//实现长按
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
[self becomeFirstResponder];
//自定义方法 系统方法在
UIMenuItem *flagItemA = [[UIMenuItem alloc] initWithTitle:@"A就是我" action:@selector(methodA:)];
UIMenuItem *flagItemB = [[UIMenuItem alloc] initWithTitle:@"我是方法B" action:@selector(methodB:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:@[flagItemA,flagItemB]];
[menu setTargetRect:self.frame inView:self.superview];
[menu setMenuVisible:YES animated:YES];
}
}
//系统自带的方法 不实现canPerformAction: withSender:默认为return NO
/*
- (void)cut:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)copy:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)paste:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)select:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)selectAll:(id)sender NS_AVAILABLE_IOS(3_0);
- (void)delete:(id)sender NS_AVAILABLE_IOS(3_2);
- (void)makeTextWritingDirectionLeftToRight:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)makeTextWritingDirectionRightToLeft:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)toggleBoldface:(id)sender NS_AVAILABLE_IOS(6_0);
- (void)toggleItalics:(id)sender NS_AVAILABLE_IOS(6_0);
- (void)toggleUnderline:(id)sender NS_AVAILABLE_IOS(6_0);
- (void)increaseSize:(id)sender NS_AVAILABLE_IOS(7_0);
- (void)decreaseSize:(id)sender NS_AVAILABLE_IOS(7_0);
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action == @selector(cut:)){
return NO;
}
else if(action == @selector(copy:)||action == @selector(paste:)){
return YES;
}else if(action == @selector(methodA:) || action == @selector(methodB:)){
return YES;
}
else{
return [super canPerformAction:action withSender:sender];
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)copy:(id)sender {
UIPasteboard *gpBoard = [UIPasteboard generalPasteboard];
NSString *theTile = self.textLabel.text;
gpBoard.string = theTile;
}
- (void)paste:(id)sender
{
[super paste:sender];
//从UIPasteboard 单例中读取内容 操作
// UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
}
- (void)cut:(id)sender
{
[super cut:sender];
}
- (void)methodA:(id)sender
{
NSLog(@"我是A");
}
- (void)methodB:(id)sender
{
NSLog(@"我是B");
}
//- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
// [[self delegate] performSelector:@selector(showMenu:)
// withObject:self];
// [super setHighlighted:highlighted animated:animated];
//}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
效果图如下:

如需了解更多iOS 复制粘贴理论知识请移步我的另一篇文章:
http://www.cnblogs.com/zpz5789/p/4870798.html
来源:https://www.cnblogs.com/zpz5789/p/4867863.html