iOS [UIPasteboard generalPasteboard]剪贴板的使用

[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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值