iOS_UITableView 编辑(cell的插入, 删除, 移动)
2015-08-14 10:09
519 查看
说明:
UITableView 中的cell的插入, 删除, 移动效果的实现, 主要通过UITableView的协议中方法实现.
文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)
此文章由 @Scott 编写. 经 @春雨,@黑子 审核. 若转载此文章,请注明出处和作者
Delegate : UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方详细注释详见本章结尾)
指定哪些 cell 可以进行编辑
指定cell的编辑状态(删除还是插入)
选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入)
2 . 指定哪些行的 cell 可以进行编辑 (UITableViewDataSource 协议方法)
3 . 指定cell的编辑状态(删除还是插入) (UITableViewDelegate 协议方法)
4 . 选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入) (UITableViewDataSource 协议方法)
Deletage: UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方详细注释详见本章结尾)
指定 tableView 哪些行(cell) 可以移动
移动 cell 后的操作: 数据源进行更新
2 . 指定 tableView 哪些行(cell) 可以移动 (UITableViewDataSource协议方法)
3 . 移动 cell 后的操作: 数据源进行更新
UITableView cell的插入删除
核心API
功能实现
思路
Code
UITableView cell 的移动
核心API
功能实现
思路
Code
API 官方注释
UITableView 中的cell的插入, 删除, 移动效果的实现, 主要通过UITableView的协议中方法实现.
文章中尽量不使用或少使用封装, 目的是让大家清楚为了实现功能所需要的官方核心API是哪些(如果使用封装, 会在封装外面加以注释)
此文章由 @Scott 编写. 经 @春雨,@黑子 审核. 若转载此文章,请注明出处和作者
UITableView cell的插入/删除
核心API
Class : UITableViewDelegate : 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
功能实现
思路:
让TableView 进入编辑状态指定哪些 cell 可以进行编辑
指定cell的编辑状态(删除还是插入)
选中删除(或插入)状态之后的操作(数据源进行更新, 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 协议方法)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { /** 不同的行, 可以设置不同的编辑样式, 编辑样式是一个枚举类型 */ if (indexPath.row == 0) { return UITableViewCellEditingStyleInsert; } else { return UITableViewCellEditingStyleDelete; } }
4 . 选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入) (UITableViewDataSource 协议方法)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { /** 点击 删除 按钮的操作 */ if (editingStyle == UITableViewCellEditingStyleDelete) { /**< 判断编辑状态是删除时. */ /** 1. 更新数据源(数组): 根据indexPaht.row作为数组下标, 从数组中删除数据. */ [self.arr removeObjectAtIndex:indexPath.row]; /** 2. TableView中 删除一个cell. */ [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; } /** 点击 +号 图标的操作. */ if (editingStyle == UITableViewCellEditingStyleInsert) { /**< 判断编辑状态是插入时. */ /** 1. 更新数据源:向数组中添加数据. */ [self.arr insertObject:@"abcd" atIndex:indexPath.row]; /** 2. TableView中插入一个cell. */ [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } }
UITableView cell 的移动
核心API
Class: UITableViewDeletage: UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方详细注释详见本章结尾)
/** TableView 进入或退出编辑状态(TableView 方法). */ - (void)setEditing:(BOOL)editing animated:(BOOL)animate /** 指定 tableView 哪些行(cell) 可以移动. */ - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath /** 移动 cell. */ - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
功能实现
思路:
让 TableView 进入或退出 编辑状态指定 tableView 哪些行(cell) 可以移动
移动 cell 后的操作: 数据源进行更新
Code
1 . 让 TableView 进入或退出 编辑状态/** 当点击UINavigationBar 上面系统提供的编辑按钮的时候, 系统会调用这个方法. */ - (void)setEditing:(BOOL)editing animated:(BOOL)animated { /** 首先调用父类的方法. */ [super setEditing:editing animated:animated]; /** 使tableView处于编辑状态. */ [self.tableView setEditing:editing animated:animated]; }
2 . 指定 tableView 哪些行(cell) 可以移动 (UITableViewDataSource协议方法)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { /** 指定哪些行(cell)可以移动 */ if (0 == indexPath.row) { return NO; /**< NO cell不能移动 */ } else { return YES; /**< YES cell可以移动 */ } }
3 . 移动 cell 后的操作: 数据源进行更新
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { /** 1. 从原位置移除,在从原位置移除之前, 需要保存一下原位置的数据, 同时持有一次. */ NSString *str = [[self.arr objectAtIndex:sourceIndexPath.row] retain]; [self.arr removeObjectAtIndex:sourceIndexPath.row]; /** 2. 添加到目的位置, 同时释放一次 */ [self.arr insertObject:str atIndex:destinationIndexPath.row]; [str release]; }
UITableView cell的插入删除
核心API
功能实现
思路
Code
UITableView cell 的移动
核心API
功能实现
思路
Code
API 官方注释
API 官方注释
/** * @brief Asks the data source to commit the insertion or deletion of a specified row in the receiver. * * @param <tableView> The table-view object requesting the insertion or deletion. * @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. * @param <indexPath> An index path locating the row in tableView. */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
/** * @brief Asks the data source to verify that the given row is editable. * * @param <tableView> The table-view object requesting this information. * @param <indexPath> An index path locating a row in tableView. * * @return YES if the row indicated by indexPath is editable; otherwise, NO. */ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
/** * @brief Inserts rows in the table view at the locations identified by an array of index paths, with an option to animate the insertion. * * @param <indexPaths> An array of NSIndexPath objects, each representing a row index and section index that together identify a row in the table view. * @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. * **/ - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
/** * @brief Deletes the rows specified by an array of index paths, with an option to animate the deletion. * * @param <indexPaths> An array of NSIndexPath objects identifying the rows to delete. * @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. * */ - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
相关文章推荐
- android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!
- android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!
- hdu 5301 Buildings
- 创建型模式:建造者模式(Builder Pattern)
- Havok_2014-1-0_Pc_Xs_User_Guide(2.3.2-一般监听器)
- UITextField-设置大全-placeholder左上角显示
- hdu - 3836 Equivalent Sets(强连通)
- popupwindow实现炫酷动画UI
- SSIS 错误代码 DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER(0xC0202009)
- systemverilog中$value$plusargs的用法
- POJ 3080 Blue Jeans
- Windows Server 2016 build 10514系统截图曝光
- java.lang.IllegalArgumentException: Illegal character in query at index ...解决办法
- Value Stewardship Over Showmanship
- UINavigationBar的系统渲染方式
- 用Ionic框架+HBuilder开发的一些观察
- UIScrollView+UIPageControl
- 根据map的value值排序并输出
- h5+ mui笔记
- h5+ mui笔记