【转】 UITableViewCell的标记、移动、删除、插入
原文: http://blog.csdn.net/duxinfeng2010/article/details/7725897
这篇文章是建立在
代码实现 UITableView与UITableViewCell基础上进行修改,用不上的代码我注释调,部分不明白可以看看上篇博客;实现的功能是对UITableViewCell的标记、移动、删除、插入;
setEditing:NO animated:YES];表示把单元格可编辑状态这只为NO
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];
- if (cellView.accessoryType == UITableViewCellAccessoryNone) {
- cellView.accessoryType=UITableViewCellAccessoryCheckmark;
- }
- else {
- cellView.accessoryType = UITableViewCellAccessoryNone;
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- }
当我们选中单元格的时候,调用此函数,首先是indexPath检测选中了哪一行,if判断当前单元格是否被标记,也就是当前单元格风格,是否为UITableViewCellAccessoryCheckmark风格,如果是,则换成UITableViewCellAccessoryNone(不被标记风格)风格,以下是accessoryType四个风格属性
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryNone
setEditing:YES animated:YES];
- //返回YES,表示支持单元格的移动
- -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- //单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleInsert;
- }
三种风格的分别是
UITableViewCellEditingStyleDelete UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone
- -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
- {
- // 需要的移动行
- NSInteger fromRow = [sourceIndexPath row];
- // 获取移动某处的位置
- NSInteger toRow = [destinationIndexPath row];
- // 从数组中读取需要移动行的数据
- id object = [self.listData objectAtIndex:fromRow];
- // 在数组中移动需要移动的行的数据
- [self.listData removeObjectAtIndex:fromRow];
- // 把需要移动的单元格数据在数组中,移动到想要移动的数据前面
- [self.listData insertObject:object atIndex:toRow];
- }
单元格的移动是选中单元格行后面三条横线才可以实现移动的
- //单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleDelete;
- }
- -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle==UITableViewCellEditingStyleDelete) {
- // 获取选中删除行索引值
- NSInteger row = [indexPath row];
- // 通过获取的索引值删除数组中的值
- [self.listData removeObjectAtIndex:row];
- // 删除单元格的某一行时,在用动画效果实现删除过程
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- }
删除了张四 效果图:
- //单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleInsert;
- }
为了显示效果明显,在.h文件中声明一个变量i
- #import <UIKit/UIKit.h>
- @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
- {
- NSInteger i;
- }
- @property(strong,nonatomic) NSMutableArray *listData;
- @property(strong,nonatomic)UITableView *tableView;
- @property(strong,nonatomic)UITableViewCell *tableViewCell;
- @end
- -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle==UITableViewCellEditingStyleDelete) {
- // 获取选中删除行索引值
- NSInteger row = [indexPath row];
- // 通过获取的索引值删除数组中的值
- [self.listData removeObjectAtIndex:row];
- // 删除单元格的某一行时,在用动画效果实现删除过程
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- if(editingStyle==UITableViewCellEditingStyleInsert)
- {
- i=i+1;
- NSInteger row = [indexPath row];
- NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];
- NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];
- // 添加单元行的设置的标题
- [self.listData insertObject:mes atIndex:row];
- [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];
- }
- }
运行效果图:
UITableViewRowAnimationAutomatic UITableViewRowAnimationTop
UITableViewRowAnimationBottom UITableViewRowAnimationLeft
UITableViewRowAnimationRight UITableViewRowAnimationMiddle
UITableViewRowAnimationFade UITableViewRowAnimationNone
//设置进入编辑状态时,Cell不会缩进
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
//使Cell显示移动按钮
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
//在下面方法中添加 cell.showsReorderControl =YES;
最新文章
- JavaIO流文件的操作总结
- 树(二)&mdash;&mdash;二叉树
- UVA 315 315 - Network(求割点个数)
- WebAPI GET和POST请求的几种方式
- Codeforces Round #329 div2
- js性能优化--学习笔记
- Conversion to Dalvik format failed with error 1(android)
- FZU Problem 1895 整除45问题(整除问题+字符串维护+优化)
- centos perl: symbol lookup error: /usr/local/lib64/perl5/auto/DBD/mysql/mysql.so: undefined symbol: mysql_init
- Class:DbConnectionManipulator.cs
- 关于JS的一些东西
- HDU - 3521 An easy Problem(矩阵快速幂)
- TERMIOS详解【转】
- npm国内镜像
- [openjudge-搜索]深度优先搜索之马走日
- 【365】拉格朗日乘子法与KKT条件说明
- linux上安装rabbitMQ
- 爬虫、网页分析解析辅助工具 Xpath-helper
- 51NOD 1081 子段求和
- http状态码学习笔记
热门文章
- Android Studio 运行、编译卡死的解决办法
- maven配置默认jdk版本
- 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property &#39;source&#39; to
- Hibernate(八)一对多单向关联映射
- idea安装Scala插件
- [cocos2d-x3.0]Android+NDK+Eclipse环境搭建及编译步骤~
- windows调用ubuntu下的sublimeText2环境搭建
- OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(三)——计算节点的安装
- 简单粗暴地理解 JavaScript 原型链
- Java Web系列:Spring依赖注入基础