原文: http://blog.csdn.net/duxinfeng2010/article/details/7725897

这篇文章是建立在

代码实现 UITableView与UITableViewCell基础上进行修改,用不上的代码我注释调,部分不明白可以看看上篇博客;实现的功能是对UITableViewCell的标记、移动、删除、插入;

1通过修改cell的accessoryType属性来实现,首先,在ViewDidLoad中[tableView
setEditing:NO animated:YES];表示把单元格可编辑状态这只为NO
  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];
  4. if (cellView.accessoryType == UITableViewCellAccessoryNone) {
  5. cellView.accessoryType=UITableViewCellAccessoryCheckmark;
  6. }
  7. else {
  8. cellView.accessoryType = UITableViewCellAccessoryNone;
  9. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  10. }
  11. }

当我们选中单元格的时候,调用此函数,首先是indexPath检测选中了哪一行,if判断当前单元格是否被标记,也就是当前单元格风格,是否为UITableViewCellAccessoryCheckmark风格,如果是,则换成UITableViewCellAccessoryNone(不被标记风格)风格,以下是accessoryType四个风格属性

 UITableViewCellAccessoryCheckmark
                UITableViewCellAccessoryDetailDisclosureButton
        
UITableViewCellAccessoryDisclosureIndicator
  UITableViewCellAccessoryNone
     
2.移动
 
实现移动单元格就需要把单元格的编辑属性设置为YES,[tableView
setEditing:YES animated:YES];
  1. //返回YES,表示支持单元格的移动
  2. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return YES;
  5. }
  1. //单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格
  2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return UITableViewCellEditingStyleInsert;
  5. }

三种风格的分别是

UITableViewCellEditingStyleDelete                                                UITableViewCellEditingStyleInsert

  

UITableViewCellEditingStyleNone

实现移动的方法
  1. -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  2. {
  3. //    需要的移动行
  4. NSInteger fromRow = [sourceIndexPath row];
  5. //    获取移动某处的位置
  6. NSInteger toRow = [destinationIndexPath row];
  7. //    从数组中读取需要移动行的数据
  8. id object = [self.listData objectAtIndex:fromRow];
  9. //    在数组中移动需要移动的行的数据
  10. [self.listData removeObjectAtIndex:fromRow];
  11. //    把需要移动的单元格数据在数组中,移动到想要移动的数据前面
  12. [self.listData insertObject:object atIndex:toRow];
  13. }

单元格的移动是选中单元格行后面三条横线才可以实现移动的

  
3.删除
首先是判断(UITableViewCellEditingStyle)editingStyle,所以
  1. //单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格
  2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return UITableViewCellEditingStyleDelete;
  5. }
  1. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. if (editingStyle==UITableViewCellEditingStyleDelete) {
  4. //        获取选中删除行索引值
  5. NSInteger row = [indexPath row];
  6. //        通过获取的索引值删除数组中的值
  7. [self.listData removeObjectAtIndex:row];
  8. //        删除单元格的某一行时,在用动画效果实现删除过程
  9. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  10. }
  11. }

删除了张四 效果图:

    
4.添加
实现方法和删除方法相同,首先还是返回单元格编辑风格
  1. //单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格
  2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. return UITableViewCellEditingStyleInsert;
  5. }

为了显示效果明显,在.h文件中声明一个变量i

  1. #import <UIKit/UIKit.h>
  2. @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
  3. {
  4. NSInteger i;
  5. }
  6. @property(strong,nonatomic) NSMutableArray *listData;
  7. @property(strong,nonatomic)UITableView *tableView;
  8. @property(strong,nonatomic)UITableViewCell *tableViewCell;
  9. @end

  1. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. if (editingStyle==UITableViewCellEditingStyleDelete) {
  4. //        获取选中删除行索引值
  5. NSInteger row = [indexPath row];
  6. //        通过获取的索引值删除数组中的值
  7. [self.listData removeObjectAtIndex:row];
  8. //        删除单元格的某一行时,在用动画效果实现删除过程
  9. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  10. }
  11. if(editingStyle==UITableViewCellEditingStyleInsert)
  12. {
  13. i=i+1;
  14. NSInteger row = [indexPath row];
  15. NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];
  16. NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];
  17. //        添加单元行的设置的标题
  18. [self.listData insertObject:mes atIndex:row];
  19. [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];
  20. }
  21. }

运行效果图:

     

在删除和添加单元格的用到UITableViewRowAnimation动画效果,它还有其他几种效果,在此不做测试

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;

 

【转】 UITableViewCell的标记、移动、删除、插入的更多相关文章

  1. Windows 服务卸载之后 重新安装提示 “指定的服务已标记为删除”

    背景:        将一个项目做成一个windows服务,在调试的时候,需要卸载.安装该服务,但提示下面的错误:“指定的服务已标记为删除”,进入服务管理界面,启动自己注册的服务,无法手动更改成启用模 ...

  2. Oracle 序列的创建删除插入

    今天学习的是序列的创建蟹盖和删除插入 创建: create Sequence Seq_name increment by n     ----序列变化的程度,默认为1,可以为负数表示递减 start ...

  3. 指定的服务已标记为删除 寒江孤钓<<windows 内核安全编程>> 学习笔记

    运行cmd:"sc delete first" 删除我们的服务之后, 再次创建这个服务的时候出现 "指定的服务已标记为删除"的错误, 原因是我们删除服务之前没有 ...

  4. UITableViewCell单元格的删除、插入、移动

    UITableViewDelegate的方法      设置编辑模式中得cell的编辑样式(删除或插入)      - (UITableViewCellEditingStyle)tableView:( ...

  5. JavaScript中的内置对象-8--1.Array(数组)-Array构造函数; 数组的栈方法; 数组的转换方法; 数组的操作方法; 删除-插入-替换数组项; ECMAScript为数组实例添加的两个位置方法;

    JavaScript内置对象-1Array(数组) 学习目标 1.掌握任何创建数组 2.掌握数值元素的读和写 3.掌握数组的length属性 如何创建数组 创建数组的基本方式有两种: 1.使用Arra ...

  6. foreach循环与迭代器循环 删除插入元素的区别

     (1)仅对其遍历而不修改容器大小时,只使用foreach循环 (2)需要边遍历边修改容器大小时(插入删除元素),只使用迭代器循环 import java.util.HashMap;import ja ...

  7. Bitter.Core系列七:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 示例 更新删除插入

    Bitter Orm 在操作数据库增删改的时候,支持模型驱动和直接执行裸SQL 操作,示例代码如下: 一:模型驱动(增删改) /// <summary> /// 插入,删除,更新示例(模型 ...

  8. sc delete 服务器名提示“指定的服务已经标记为删除”

    症状:停止服务后,右键无法点击“启动”,打开命令输入SC删除服务后提示如标题描述 原因:进程还在 解决方案:打开任务管理器,进程,找到你的已停止服务名,右键,结束进程,再操作即可删除服务

  9. IOS uitableviewcell 向左滑动删除编辑等

    主要实现这个方法就好了 -(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActions ...

随机推荐

  1. 14.6.2 Configuring InnoDB for Read-Only Operation

    14.6.2 Configuring InnoDB for Read-Only Operation 配置InnoDB只读操作 你可以查询InnoDB 表MySQL 数据目录是一个只读介质,通过启用 - ...

  2. bzoj3504

    这是一道最大流的题目首先要引起注意的是,这类多个起点多个终点的问题一定要同时跑,不能分开来跑由于是无向图,也就相当于从起点跑2*n次好,不难想到s向两个起点连边,两终点想t连边,流量为2*an或2*b ...

  3. codeforces -- 283A

    A. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  4. MySQL中的类Decode用法

    SELECT DISTINCT ( CASE ' THEN '关羽' ' THEN ' 张飞' ' THEN ' 赵云' ' THEN ' 马超' ' THEN ' 黄忠' ' THEN ' 魏延' ...

  5. springMVC能做什么,做j2ee时候要考虑什么

    转载: http://jinnianshilongnian.iteye.com/category/231099 [置顶] 跟我学SpringMVC目录汇总贴.PDF下载.源码下载 博客分类: 跟开涛学 ...

  6. 数学常数e的含义

    转载:   http://www.ruanyifeng.com/blog/2011/07/mathematical_constant_e.html 作者: 阮一峰 日期: 2011年7月 9日 1. ...

  7. Space Ant - POJ 1696 (凸包)

    题目大意:给一些散列点然后初始点是坐标最下面最左面的点,然后只能往左走,求出来最多可以经过多少个点,把序号输出出来.   分析:先求出来初始的点,然后不断排序找出来最近的凸点....复杂度是 n^2* ...

  8. centos 6.5上部署jetty

    和tomcat是一样的,在部署容器之前,我们首先得有java环境 这里我们选择用rpm包的方式安装jdk 这里我们上传我之前下载好的jdk包 然后按照该文章http://blog.csdn.net/x ...

  9. Adatper中获取宽高为0的问题

    但是我们想在getView()中获取ImageView的宽和高存在问题,在getView()里面刚开始显示item的时候利用ImageView.getWidth() 获取的都是0,为什么刚开始获取不到 ...

  10. PPT扁平化手册 2