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

    xiaoxiao2025-11-01  8

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

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

    <span style="font-size:14px;">#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (strong, nonatomic) UITableView *myTableView; @property (strong, nonatomic) NSMutableArray *dataArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.dataArray = [NSMutableArray arrayWithObjects:@"孙悟空",@"唐僧",@"沙和尚",@"猪悟能",@"红孩儿",@"牛魔王",@"蜘蛛精",@"白骨精",@"宋江",@"鲁智深",@"李逵",@"武松",@"贾宝玉", nil]; self.myTableView =[[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; self.myTableView.dataSource = self; self.myTableView.delegate = self; [self.view addSubview:self.myTableView]; //右上角的编辑按钮 self.navigationItem.rightBarButtonItem = self.editButtonItem; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; [self.myTableView setEditing:!self.myTableView.isEditing animated:YES]; } #pragma mark - table view data source and delegate //多少个分区 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } //多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } //行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 65.0f; } //配置cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"cellID"; UITableViewCell *cell; cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = self.dataArray[indexPath.row]; return cell; } //设置table view 为可编辑的 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //设置可编辑的样式 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete; } //设置处理编辑情况 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { //更新数据 [self.dataArray removeObjectAtIndex:indexPath.row]; //更新UI [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } } //设置可移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //处理移动的情况 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ //更新数据 NSString *title = self.dataArray[sourceIndexPath.row]; [self.dataArray removeObject:title]; [self.dataArray insertObject:title atIndex:destinationIndexPath.row]; //更新UI [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]; } #pragma mark - 返回按钮/处理按钮的点击事件 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ //添加一个删除按钮 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"点击了删除"); //1.更新数据 [self.dataArray removeObjectAtIndex:indexPath.row]; //2.更新UI [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }]; //添加一个更多按钮 UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"点击了更多"); [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; }]; //添加一个置顶按钮 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { NSLog(@"你点击了置顶按钮"); NSString *title = [self.dataArray objectAtIndex:indexPath.row]; [self.dataArray removeObject:title]; [self.dataArray insertObject:title atIndex:0]; [tableView reloadData]; }]; moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; //将设置好的按钮存放到数组中(按钮对象在数组中的索引从0到最多,在tableViewCell中的显示则是从右到左依次排列) return @[deleteRowAction,moreRowAction,topRowAction]; }</span><span style="color:#ff0000;font-size: 32px;"> </span> 实现效果如下图所示:

    转载请注明原文地址: https://ju.6miu.com/read-1303734.html
    最新回复(0)