[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 = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in tempArray) {
HerosModel *model = [HerosModel HerosModelWithDictionary:dict];
[_dataArray addObject:model];
}
}
return _dataArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.rowHeight = 60;
[self.view addSubview:_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 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
HerosModel *model = self.dataArray[indexPath.row];
cell.imageView.image = [UIImage imageNamed: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:(nullable id)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:(nullable id)sender {
if (action == @selector(copy:)) {
// copy 的数据是 被点击这一行的内容
// 1. 取出被点击cell对应的Model
HerosModel *heroModel = self.dataArray[indexPath.row];
// 2. 把内容保存到剪贴板上 , 剪贴板是属于全局的
[UIPasteboard generalPasteboard].strings = @[heroModel.icon,heroModel.name,heroModel.intro];
} else if (action == @selector(paste:)) {
// 1. 取出剪贴板中保存的内容
NSArray *contentArray = [UIPasteboard generalPasteboard].strings;
// 2. 实例化一个heroModel 对象
HerosModel *heroModel = [[HerosModel alloc] init];
heroModel.icon = contentArray[0];
heroModel.name = contentArray[1];
heroModel.intro = contentArray[2];
// 3. 刷新数据源
[_dataArray insertObject:heroModel atIndex:indexPath.row];
[_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
@end