iOS8新添加的左滑出现按钮组的方法

 

iOS8新添加的左滑出现按钮组的方法 

  10人阅读  评论(0)  收藏  举报
  分类:
 

iOS8以后table view中添加了可以左滑出现按钮组的方法,如下图:


关于此方法的用法就直接上代码:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">#import "ViewController.h"  
  2.   
  3. @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>  
  4. @property (strongnonatomicUITableView *myTableView;  
  5. @property (strongnonatomicNSMutableArray *dataArray;  
  6.   
  7. @end  
  8.   
  9. @implementation ViewController  
  10.   
  11. - (void)viewDidLoad {  
  12.     [super viewDidLoad];  
  13.   
  14.     self.dataArray = [NSMutableArray arrayWithObjects:@"孙悟空",@"唐僧",@"沙和尚",@"猪悟能",@"红孩儿",@"牛魔王",@"蜘蛛精",@"白骨精",@"宋江",@"鲁智深",@"李逵",@"武松",@"贾宝玉", nil nil];  
  15.     self.myTableView =[[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];  
  16.     self.myTableView.dataSource  = self;  
  17.     self.myTableView.delegate = self;  
  18.     [self.view addSubview:self.myTableView];  
  19.     //右上角的编辑按钮  
  20.     self.navigationItem.rightBarButtonItem = self.editButtonItem;  
  21.   
  22.     // Do any additional setup after loading the view, typically from a nib.  
  23. }  
  24.   
  25. - (void)didReceiveMemoryWarning {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28. }  
  29.   
  30.   
  31.   
  32. - (void)setEditing:(BOOL)editing animated:(BOOL)animated{  
  33.     [super setEditing:editing animated:animated];  
  34.     [self.myTableView setEditing:!self.myTableView.isEditing animated:YES];  
  35. }  
  36.   
  37.   
  38. #pragma mark - table view data source and delegate  
  39.   
  40. //多少个分区  
  41. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  42.     return 1;  
  43. }  
  44.   
  45. //多少行  
  46. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  47.     return self.dataArray.count;  
  48. }  
  49.   
  50. //行高  
  51. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  52.     return 65.0f;  
  53. }  
  54.   
  55. //配置cell  
  56. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  57.     static NSString *cellIdentifier = @"cellID";  
  58.     UITableViewCell *cell;  
  59.     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];  
  60.     if (!cell) {  
  61.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];  
  62.     }  
  63.     cell.textLabel.text = self.dataArray[indexPath.row];  
  64.       
  65.     return cell;  
  66. }  
  67.   
  68. //设置table view 为可编辑的  
  69. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{  
  70.     return YES;  
  71. }  
  72.   
  73. //设置可编辑的样式  
  74. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{  
  75.     return UITableViewCellEditingStyleDelete;  
  76. }  
  77.   
  78. //设置处理编辑情况  
  79. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{  
  80.     if (editingStyle == UITableViewCellEditingStyleDelete) {  
  81.         //更新数据  
  82.         [self.dataArray removeObjectAtIndex:indexPath.row];  
  83.         //更新UI  
  84.         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  85.     }  
  86. }  
  87.   
  88. //设置可移动  
  89. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{  
  90.     return YES;  
  91. }  
  92.   
  93. //处理移动的情况  
  94. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{  
  95.     //更新数据  
  96.     NSString *title = self.dataArray[sourceIndexPath.row];  
  97.     [self.dataArray removeObject:title];  
  98.     [self.dataArray insertObject:title atIndex:destinationIndexPath.row];  
  99.     //更新UI  
  100.     [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];  
  101. }  
  102.   
  103.   
  104. #pragma mark - 返回按钮/处理按钮的点击事件  
  105. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{  
  106.       
  107.     //添加一个删除按钮  
  108.     UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {  
  109.         NSLog(@"点击了删除");  
  110.         //1.更新数据  
  111.         [self.dataArray removeObjectAtIndex:indexPath.row];  
  112.         //2.更新UI  
  113.         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  114.     }];  
  115.       
  116.     //添加一个更多按钮  
  117.     UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {  
  118.         NSLog(@"点击了更多");  
  119.         [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];  
  120.           
  121.     }];  
  122.     //添加一个置顶按钮  
  123.     UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {  
  124.         NSLog(@"你点击了置顶按钮");  
  125.         NSString *title = [self.dataArray objectAtIndex:indexPath.row];  
  126.         [self.dataArray removeObject:title];  
  127.         [self.dataArray insertObject:title atIndex:0];  
  128.         [tableView reloadData];  
  129.     }];  
  130.       
  131.     moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];  
  132.     //将设置好的按钮存放到数组中(按钮对象在数组中的索引从0到最多,在tableViewCell中的显示则是从右到左依次排列)  
  133.     return @[deleteRowAction,moreRowAction,topRowAction];  
  134. }</span><span style="color:#ff0000;font-size: 32px;">  
  135. </span>  

实现效果如下图所示:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值