[UIPasteboard generalPasteboard]剪贴板的使用
#import "ViewController.h"
#import "HerosModel.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end
@implementation ViewController
- (NSMutableArray *)dataArray {
if (nil ==_dataArray) {
_dataArray = [NSMutableArrayarray];
NSString *path = [[NSBundlemainBundle] pathForResource:@"heros.plist"ofType:nil];
NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];
for (NSDictionary *dictin tempArray) {
HerosModel *model = [HerosModelHerosModelWithDictionary:dict];
[_dataArray addObject:model];
}
}
return_dataArray;
}
- (void)viewDidLoad {
[superviewDidLoad];
_tableView = [[UITableViewalloc] initWithFrame:self.view.bounds];
_tableView.dataSource =self;
_tableView.delegate =self;
_tableView.rowHeight =60;
[self.viewaddSubview:_tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];
HerosModel *model = self.dataArray[indexPath.row];
cell.imageView.image = [UIImageimageNamed:model.icon];
cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.intro;
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark -
#pragma mark - 要不要显示出复制/粘贴菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
// 第一行长按cell后不会出现菜单
if (indexPath.row ==0) {
return NO;
} else {
return YES;
}
}
#pragma mark -
#pragma mark - 决定菜单上显示的名称
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullableid)sender {
// 菜单中只有 copy 和 past会显示出来
if (action == @selector(copy:) || action == @selector(paste:)) {
return YES;
} else {
return NO;
}
}
/**
@property(nullable,nonatomic,copy) NSString *string;
@property(nullable,nonatomic,copy) NSArray<NSString *> *strings;
*/
#pragma mark -
#pragma mark - 点击菜单中选项会调用的方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullableid)sender {
if (action == @selector(copy:)) {
// copy 的数据是 被点击这一行的内容
// 1. 取出被点击cell对应的Model
HerosModel *heroModel = self.dataArray[indexPath.row];
// 2.把内容保存到剪贴板上 , 剪贴板是属于全局的
[UIPasteboardgeneralPasteboard].strings =@[heroModel.icon,heroModel.name,heroModel.intro];
} else if (action ==@selector(paste:)) {
// 1. 取出剪贴板中保存的内容
NSArray *contentArray = [UIPasteboardgeneralPasteboard].strings;
// 2.实例化一个heroModel对象
HerosModel *heroModel = [[HerosModelalloc] init];
heroModel.icon = contentArray[0];
heroModel.name = contentArray[1];
heroModel.intro = contentArray[2];
// 3. 刷新数据源
[_dataArray insertObject:heroModel atIndex:indexPath.row];
[_tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
}
@end