美文网首页
tableView-删除、插入、多选删除

tableView-删除、插入、多选删除

作者: CoderCurtis | 来源:发表于2017-07-26 14:27 被阅读81次
  • 左滑删除
//声明
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;//数据源

//懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//添加tableView
- (void)createUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(@0);
        make.top.equalTo(self.mas_topLayoutGuide);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
}

//数据
- (void)loadData
{
    for (int i = 0; i < 20; i ++) {
        [self.dataArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
}

//数据源代理 -展示数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}

//删除代理
//是否可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
        [self.dataArray removeObjectAtIndex:indexPath.row];//删除数据源中相应数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

//修改删除操作title -默认 "删除"(Delete) -跟随系统语言
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"左滑删除";
}

左滑删除.gif
  • 编辑状态下删除

与左滑返回区别: 设置编辑状态为YES

self.tableView.editing = !self.tableView.editing;

//编辑风格为删除
if (self.editingStyle == CCTableViewCellEditingStyleDelete) {
            return UITableViewCellEditingStyleDelete;
        }

点击编辑,出现减号,再点击减号 出现左滑返回按钮 点击左滑返回按钮 删除数据

编辑删除.gif
  • 编辑状态下插入
//编辑风格为插入
if (self.editingStyle == CCTableViewCellEditingStyleInsert) {
            return UITableViewCellEditingStyleInsert;
        }

//插入操作
[self.dataArray insertObject:@"cc" atIndex:indexPath.row];//插入数据
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
编辑插入.gif
  • 编辑风格为删除和插入共存
if (self.editingStyle == (CCTableViewCellEditingStyleDelete | CCTableViewCellEditingStyleInsert)) {
            return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
        }

//声明
@property (nonatomic, strong) NSMutableDictionary *deleteDic;//记录多选删除数据
@property (nonatomic, strong) UIBarButtonItem *deleteBtn;

//显示删除按钮
if (self.editingStyle == (CCTableViewCellEditingStyleInsert | CCTableViewCellEditingStyleDelete)) {
        
        [self.deleteDic removeAllObjects];
        
        self.deleteBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnAction)];
        NSMutableArray *Items = [[NSMutableArray alloc]initWithObjects:self.deleteBtn, nil];
        
        if (self.tableView.editing) {
            [self.navigationController setToolbarHidden:NO animated:YES];
        }else {
            [self.navigationController setToolbarHidden:YES animated:YES];
        }
        
        [self setToolbarItems:Items];
        self.deleteBtn.enabled = NO;
    }

//点击cell选择删除行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //indexPath为key 对应数据为值
    [self.deleteDic setObject:_dataArray[indexPath.row] forKey:indexPath];
    if (self.deleteDic.count == 0) {
        self.deleteBtn.enabled = NO;
    } else {
        self.deleteBtn.enabled = YES;
    }
}

//取消行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.deleteDic removeObjectForKey:indexPath];
    if (self.deleteDic.count == 0) {
        self.deleteBtn.enabled = NO;
    } else {
        self.deleteBtn.enabled = YES;
    }
}

//删除操作
//多选删除操作
    [_dataArray removeObjectsInArray:[self.deleteDic allValues]];
    [self.tableView deleteRowsAtIndexPaths:[self.deleteDic allKeys] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.deleteDic removeAllObjects];
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationController setToolbarHidden:YES animated:YES];
多选删除.gif

左滑删除很多应用中都会此功能,而其他的系统自带功能或许并不一定符合实际需求;关于多选删除,过几天会分享一个小例子

代码

相关文章

  • tableView-删除、插入、多选删除

    左滑删除 编辑状态下删除 与左滑返回区别: 设置编辑状态为YES 点击编辑,出现减号,再点击减号 出现左滑返回按钮...

  • 第2单元 建立工作表——编辑工作表

    1、插入和删除区域 2、插入列 插入单元格 删除行列 删除单元格 删除内容 重命名工作表 移动工作表 删除工作表 ...

  • JS示例18-数组基本使用

    一、声明 二、属性length 设置为0,可以快速清空数组 三、插入删除 尾部插入删除 头部插入删除 指定位置插入...

  • splice的用法删除、插入、替换

    splice可以用作删除、插入、替换功能 删除 插入 替换

  • mogodb 基本常用sql操作

    插入 插入一条数据 插入多条数据 可以插入一条也可以插入多条,返回WriteResult 删除 删除集合 删除一条...

  • 5、工作表的基本操作

    1、工作表的选择 单选、多选、挑选、连选、右键选择全部工作表 2、新建工作表 表中点、插入、功能区插入 3、删除 ...

  • 数据结构动画描述

    数组 插入数组插入 删除数组删除 链表 栈 队列 二分搜索树 插入

  • 插入/删除

    查看数据库里集合 show collections 删除集合 db.students.drop() 创建集合 一般...

  • Notepad++ 操作技巧

    一、删除奇数行 二、删除偶数行 三、删除以xyz开头的行 四、每行的开头插入 五、每行的结尾插入 六、删除空行

  • 5. B+ 树

    插入 删除

网友评论

      本文标题:tableView-删除、插入、多选删除

      本文链接:https://www.haomeiwen.com/subject/gcankxtx.html