UITableView Cell的插入/删除

核心API

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

  1. /** TableView 进入或退出编辑状态(TableView 方法). */
  2. - (void)setEditing:(BOOL)editing animated:(BOOL)animate
  3.  
  4. /** 确定哪些行的cell可以编辑 (UITableViewDataSource协议中方法). */
  5. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  6.  
  7. /** 设置某一行cell的编辑模式 (UITableViewDelegate协议中方法). */
  8. TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  9.  
  10. /** 提交编辑状态 (UITableViewDataSource协议中方法). */
  11. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  12.  
  13. /** 插入 cell (UITableView 方法). */
  14. - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
  15.  
  16. /** 删除 cell (UITableView 方法). */
  17. - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

功能实现
思路:

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

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

  1. /** 当点击UINavigationBar 上面系统提供的编辑按钮的时候, 系统会调用这个方法. */
  2. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  3. {
  4. /** 首先调用父类的方法. */
  5. [super setEditing:editing animated:animated];
  6.  
  7. /** 使tableView处于编辑状态. */
  8. [self.tableView setEditing:editing animated:animated];
  9.  
  10. }

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

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

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

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. /** 不同的行, 可以设置不同的编辑样式, 编辑样式是一个枚举类型 */
  4. if (indexPath.row == 0) {
  5. return UITableViewCellEditingStyleInsert;
  6. } else {
  7. return UITableViewCellEditingStyleDelete;
  8. }
  9. }

4.选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入) (UITableViewDataSource 协议方法)

  1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. /** 点击 删除 按钮的操作 */
  4. if (editingStyle == UITableViewCellEditingStyleDelete) { /**< 判断编辑状态是删除时. */
  5.  
  6. /** 1. 更新数据源(数组): 根据indexPaht.row作为数组下标, 从数组中删除数据. */
  7. [self.arr removeObjectAtIndex:indexPath.row];
  8.  
  9. /** 2. TableView中 删除一个cell. */
  10. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
  11. }
  12.  
  13. /** 点击 +号 图标的操作. */
  14. if (editingStyle == UITableViewCellEditingStyleInsert) { /**< 判断编辑状态是插入时. */
  15. /** 1. 更新数据源:向数组中添加数据. */
  16. [self.arr insertObject:@"abcd" atIndex:indexPath.row];
  17.  
  18. /** 2. TableView中插入一个cell. */
  19. [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  20.  
  21. }
  22. }

UITableView cell 的移动

核心API

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

  1. /** TableView 进入或退出编辑状态(TableView 方法). */
  2. - (void)setEditing:(BOOL)editing animated:(BOOL)animate
  3.  
  4. /** 指定 tableView 哪些行(cell) 可以移动. */
  5. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  6.  
  7. /** 移动 cell. */
  8. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

功能实现

思路:

  1. 1. TableView 进入或退出 编辑状态
  2. 2.指定 tableView 哪些行(cell) 可以移动
  3. 3.移动 cell 后的操作: 数据源进行更新 

Code

1 . 让 TableView 进入或退出 编辑状态

  1. /** 当点击UINavigationBar 上面系统提供的编辑按钮的时候, 系统会调用这个方法. */
  2. - (void)setEditing:(BOOL)editing animated:(BOOL)animated
  3. {
  4. /** 首先调用父类的方法. */
  5. [super setEditing:editing animated:animated];
  6.  
  7. /** 使tableView处于编辑状态. */
  8. [self.tableView setEditing:editing animated:animated];
  9. }

2.指定 tableView 哪些行(cell) 可以移动 (UITableViewDataSource协议方法)

  1. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. /** 指定哪些行(cell)可以移动 */
  4. if (0 == indexPath.row) {
  5. return NO; /**< NO cell不能移动 */
  6. } else {
  7. return YES; /**< YES cell可以移动 */
  8. }
  9. }

3.移动 cell 后的操作: 数据源进行更新

  1. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  2. {
  3. /** 1. 从原位置移除,在从原位置移除之前, 需要保存一下原位置的数据, 同时持有一次. */
  4. NSString *str = [[self.arr objectAtIndex:sourceIndexPath.row] retain];
  5.  
  6. [self.arr removeObjectAtIndex:sourceIndexPath.row];
  7.  
  8. /** 2. 添加到目的位置, 同时释放一次 */
  9. [self.arr insertObject:str atIndex:destinationIndexPath.row];
  10. [str release];
  11. }

 API 官方注释

  1. /**
  2. * @brief Asks the data source to commit the insertion or deletion of a specified row in the receiver.
  3. *
  4. * @param <tableView> The table-view object requesting the insertion or deletion.
  5. * @param <editingStyle> The cell editing style corresponding to a insertion or deletion requested for the row specified by indexPath. Possible editing styles are UITableViewCellEditingStyleInsert or UITableViewCellEditingStyleDelete.
  6. * @param <indexPath> An index path locating the row in tableView.
  7. */
  8. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  1. /**
  2. * @brief Asks the data source to verify that the given row is editable.
  3. *
  4. * @param <tableView> The table-view object requesting this information.
  5. * @param <indexPath> An index path locating a row in tableView.
  6. *
  7. * @return YES if the row indicated by indexPath is editable; otherwise, NO.
  8. */
  9. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  1. /**
  2. * @brief Inserts rows in the table view at the locations identified by an array of index paths, with an option to animate the insertion.
  3. *
  4. * @param <indexPaths> An array of NSIndexPath objects, each representing a row index and section index that together identify a row in the table view.
  5. * @param <animation> A constant that either specifies the kind of animation to perform when inserting the cell or requests no animation. See Table Cell Insertion and Deletion Animation for descriptions of the constants.
  6. *
  7. **/
  8.  
  9. - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
  1. /**
  2. * @brief Deletes the rows specified by an array of index paths, with an option to animate the deletion.
  3. *
  4. * @param <indexPaths> An array of NSIndexPath objects identifying the rows to delete.
  5. * @param <animation> A constant that indicates how the deletion is to be animated, for example, fade out or slide out from the bottom. See Table Cell Insertion and Deletion Animation for descriptions of these constants.
  6. *
  7. */
  8. - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

iOS - UITableView 编辑(cell的插入, 删除, 移动)的更多相关文章

  1. iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

    "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的 ...

  2. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  3. ios cell左滑删除

    iOS项目开发小技能 (三) -UITableView实现Cell左划删除等自定义功能 www.MyException.Cn  网友分享于:2015-06-05  浏览:0次   iOS项目开发小技巧 ...

  4. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  5. iOS学习30之UITableView编辑

    1. UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事 ...

  6. iOS学习之UITableView编辑

    一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...

  7. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  8. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  9. 设置UITableView背景透明/监听cell左边的删除按钮的点击事件

    _tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _ ...

随机推荐

  1. ios开发之--开发中可能会用到的一些函数

    rand() ----随机数 abs() / labs() ----整数绝对值 fabs() / fabsf() / fabsl() ----浮点数绝对值 floor() / floorf() / f ...

  2. VIM_manual

    VIM命令---Vi IMproved, a programmers text editor文本编辑 vim不同模式切换 输入模式 末行模式 光标移动 复制-粘贴-删除 可视模式 末行模式下的操作 v ...

  3. CM和CDH的安装-进阶完成

    安装Cloudera Manager Server 和Agent 1.在cdh1解压cloudera-manager-el6-cm5.9.0_x86_64.tar.gz(cdh1节点)tar -zcv ...

  4. 【RF库Collections测试】Remove Duplicates

    Name:Remove DuplicatesSource:Collections <test library>Arguments:[ list_ ]Returns a list witho ...

  5. mssql注入指令

    and exists (select * from sysobjects) //判断是否是MSSQL and exists(select * from tableName) //判断某表是否存在..t ...

  6. Win10 如何安装 Ubuntu

    在 Microsoft Store 中安装 Ubuntu ( 如下图1 ) 把开发者模式打开 ( 如下图2 ) 把 WSL ( Windows下的Linux子系统 ) 打开并重启电脑 ( 如下图3 )

  7. gearman 简介

    附件  Gearman.doc 1:介绍gearman 1.1 简介 Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在 ...

  8. 【基础知识】列一下一个.Net WEB程序员需要掌握的知识

    基础部分 C# 基础语法 OOP的概念,面向对象的理解 继承 封装 多态 ASP.NET MVC (Web Form 用的越来越少,如果你不熟悉,可以不看) JavaScript 基础语法 如何在HT ...

  9. Eclipse中如何在指定工程中搜索指定的字符串

    1.点击Search: 2.在下拉框中先择Search. 3. 4. 5.选择Java 6.

  10. Ajax提交表单时验证码自动验证 php后端验证码检测

    本文通过源码展示如何实现表单提交前,验证码先检测正确性,不正确则不提交表单,更新验证码. 1.前端代码 index.html <!DOCTYPE html> <html> &l ...