iOS_UITableView 编辑(cell的插入, 删除, 移动)

iOS_UITableView 编辑(cell的插入, 删除, 移动) 说明: UITableView 中的cell的插入, 删除, 移动效果的实现, 主要通过UITableView的协议中方法实现. 文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释) 此文章由 @Scott 编写. 经
摘要由CSDN通过智能技术生成

说明:
UITableView 中的cell的插入, 删除, 移动效果的实现, 主要通过UITableView的协议中方法实现.

文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)

  • 此文章由 @Scott 编写. 经 @春雨,@黑子 审核. 若转载此文章,请注明出处和作者

UITableView cell的插入/删除

核心API

Class : UITableView
Delegate : UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方详细注释详见本章结尾)

/** TableView 进入或退出编辑状态(TableView 方法). */
- (void)setEditing:(BOOL)editing animated:(BOOL)animate

/** 确定哪些行的cell可以编辑 (UITableViewDataSource协议中方法). */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

/** 设置某一行cell的编辑模式 (UITableViewDelegate协议中方法). */
TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

/** 提交编辑状态 (UITableViewDataSource协议中方法). */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

/** 插入 cell (UITableView 方法). */
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

/** 删除 cell (UITableView 方法). */
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

功能实现

思路:
  1. 让TableView 进入编辑状态
  2. 指定哪些 cell 可以进行编辑
  3. 指定cell的编辑状态(删除还是插入)
  4. 选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入)
Code:

1 . 让TableView 进入编辑状态 (UIViewControll.m)

/** 当点击UINavigationBar 上面系统提供的编辑按钮的时候, 系统会调用这个方法. */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    /** 首先调用父类的方法. */
    [super setEditing:editing animated:animated];

    /** 使tableView处于编辑状态. */
    [self.tableView setEditing:editing animated:animated];

}

2 . 指定哪些行的 cell 可以进行编辑 (UITableViewDataSource 协议方法)

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        return NO;  /**< 第一行不能进行编辑. */
    } else { 
        return YES;
    }
}

3 . 指定cell的编辑状态(删除还是插入) (UITableViewDelegate 协议方法)

- (UI
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值