UITableView-批量操作

  • 点击编辑按钮进入编辑模式
    // 进入编辑模式
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
    // 允许在编辑模式进行多选操作
    self.tableView.allowsMultipleSelectionDuringEditing = YES;

     

  • UITableView的代理方法

    /**
      * 只要实现这个方法,左划cell出现删除按钮的功能就有了
      * 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
      */
      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if (editingStyle == UITableViewCellEditingStyleDelete) {  // 点击了“删除”
              // 删除模型
              [self.deals removeObjectAtIndex:indexPath.row];
    
              // 刷新表格
      //        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
              [self.tableView reloadData];
          } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+
              NSLog(@"+++++ %zd", indexPath.row);
          }
      }
      /**
      * 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
       */
      - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert:     UITableViewCellEditingStyleDelete;
      }

     

    这个方法可以设置左滑功能的样式

      - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
          UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"增加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
          // 点击增加的时候调用
          NSLog(@"增加");
    
      }];
      action.backgroundColor = [UIColor greenColor];
    
      UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
          // 点击删除的时候调用
          NSLog(@"删除");
      }];
      return @[action,action1];
    }

posted on 2016-05-07 10:51  Enroute  阅读(192)  评论(0编辑  收藏  举报

导航