UITableView表视图编辑

表视图编辑的使用场景

当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话

当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置

代理AppDelegate.m中代码

  1. #import "AppDelegate.h"
  2. #import "RootViewController.h"
  3. @implementation AppDelegate
  4. -(void)dealloc
  5. {
  6. [_window release];
  7. [super dealloc];
  8. }
  9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  10. {
  11. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  12. // Override point for customization after application launch.
  13. self.window.backgroundColor = [UIColor whiteColor];
  14.  
  15. RootViewController *rootVC = [[RootViewController alloc] init];
  16. UINavigationController *ngVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
  17. self.window.rootViewController = ngVC;
  18.  
  19. [ngVC release];
  20. [rootVC release];
  21.  
  22. [self.window makeKeyAndVisible];
  23. return YES;
  24. }

RootViewController.h中建立一个可变数组属性

NSMutableArray *_mArr;

RootViewController.m中初始化和loadView代码

  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  2. {
  3. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  4. if (self) {
  5. self.navigationItem.title = @"百家讲坛";
  6. self.navigationItem.rightBarButtonItem = self.editButtonItem;//控制器自带的编辑按钮
  7. _mArr = [[NSMutableArray alloc] initWithObjects:@"赵",@"钱",@"孙",@"李",@"周",@"武",@"郑",@"王",@"唐僧",@"孙悟空",@"猪八戒",@"沙僧",@"小白龙",@"二郎神",@"哪吒",@"雷震子", nil];
  8. // Custom initialization
  9. }
  10. return self;
  11. }
  12. -(void)loadView
  13. {
  14.  
  15. UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStyleGrouped];
  16. table.dataSource = self;
  17. table.delegate = self;
  18. self.view = table;
  19. [table release];
  20.  
  21. }
  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  2. {
  3. return [_mArr count];
  4. }
  5.  
  6. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  7. {
  8. static NSString *identifier = @"reuse";
  9. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  10. if (cell == nil) {
  11. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  12. }
  13. cell.textLabel.text = [_mArr objectAtIndex:indexPath.row];
  14. // tableView.editing = YES;
  15. //cell右侧属性
  16. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  17. return cell;
  18. }

编辑的步骤

1.让tableView处于编辑状态

  1. -(void)setEditing:(BOOL)editing animated:(BOOL)animated
  2. {
  3. //调用父类方法,实现edit和done的变换
  4. [super setEditing:editing animated:animated];
  5. UITableView *tableView = (UITableView *)self.view;
  6. [tableView setEditing:editing animated:animated];
  7. }

2.指定tableView那些行可以编辑

  1. //设置cell的可编辑状态,默认是yes
  2. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. // if (indexPath.row == 0) {
  5. // return YES;
  6. // }
  7. return YES;
  8. }

3.指定tableView的编辑的样式(添加.删除)

  1. //delegate中得方法
  2. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. if (indexPath.row ==) {
  5. return UITableViewCellEditingStyleInsert;//添加
  6. }
  7. return UITableViewCellEditingStyleDelete;//删除
  8. }

4.编辑完成(先操作数据源,后修改UI)

  1. //点击加号或者delete时触发的事件
  2. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. if (editingStyle == UITableViewCellEditingStyleDelete) {
  5. [tableView beginUpdates];
  6. //删除数据 (写在删除cell前面 或者写一个[tableView beginUpdates]放前面一个[tableView endUpdates]放后面)
  7. [_mArr removeObjectAtIndex:indexPath.row];
  8. //删除cell
  9. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
  10. [tableView endUpdates];
  11. NSLog(@"删除");
  12. }else{
  13. [tableView beginUpdates];
  14. [_mArr insertObject:@"hello" atIndex:indexPath.row];
  15. [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
  16. [tableView endUpdates];
  17.  
  18. NSLog(@"添加");
  19. }
  20. }

表视图的移动

移动的步骤

1.让tableView处于编辑状态

  1. -(void)setEditing:(BOOL)editing animated:(BOOL)animated
  2. {
  3. //调用父类方法,实现edit和done的变换
  4. [super setEditing:editing animated:animated];
  5. UITableView *tableView = (UITableView *)self.view;
  6. [tableView setEditing:editing animated:animated];
  7. }

2.指定tableView哪些行可以移动

  1. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. return YES;
  4. }

3.移动完成

  1. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  2. {
  3. NSString *str = [_mArr objectAtIndex:sourceIndexPath.row];
  4.  
  5. //引用计数加1.避免出现野指针
  6. [str retain];
  7.  
  8. //删除元素
  9. [_mArr removeObjectAtIndex:sourceIndexPath.row];
  10. //插入元素
  11. [_mArr insertObject:str atIndex:destinationIndexPath.row];
  12.  
  13. [str release];//释放之前,retain的对象
  14. }

监测移动过程,实现限制跨区移动

  1. - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
  2. {
  3. NSLog(@"%d",sourceIndexPath.row);
  4. NSLog(@"%d",proposedDestinationIndexPath.row);
  5. if (sourceIndexPath.row == [_mArr count]-) {
  6. return sourceIndexPath;
  7. }else{
  8. return proposedDestinationIndexPath;
  9. }
  10. }

UITableViewController表视图控制器

继承自UIViewController

自带一个tableView,根视图就是tableView

模板自带编辑移动相关的代码

UI学习笔记---第十天UITableView表视图编辑的更多相关文章

  1. UI学习笔记---第十一天UITableView表视图高级-自定义cell

    自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代 ...

  2. UITableView 表视图编辑

    UITableViewController(表视图控制器)继承自UIViewController,自带一个tableView self.view不是UIView而是UITableView dataso ...

  3. ui学习笔记---第十五天数据库

    数据库的使用 常见的数据库有MySQL       SQL Server       SQLite      Oralce等 在iOS开发中通常使用SQLite数据库,这是一个轻量级的数据库,可以在火 ...

  4. JavaScript高级程序设计学习笔记第十四章--表单

    1.在 HTML 中,表单是由<form>元素来表示的,而在 JavaScript 中,表单对应的则是 HTMLFormElement 类型. HTMLFormElement 继承了 HT ...

  5. UI学习笔记---第十六天XML JSON解析

    一.解析的基本概念 从事先规定好的格式中提取数据 解析的前提:提前约定好格式.数据提供方按照格式提供数据,数据方按照格式获取数据 常见解析方式XML解析JSON解析 二.XML:可扩展标记语言 XML ...

  6. UI学习笔记---第十四天数据持久化

    一.沙盒机制 每个应用程序位于文件系统的严格限制部分 每个应用程序只能在为该程序创建的文件系统中读取文件 每个应用程序在iOS系统内斗放在了统一的文件夹目录下 沙盘路径的位置 1. 通过Finder查 ...

  7. Spring 学习笔记(十)渲染 Web 视图 (Apache Tilesa 和 Thymeleaf)

    使用Apache Tiles视图定义布局 为了在Spring中使用Tiles,需要配置几个bean.我们需要一个TilesConfigurer bean,它会负责定位和加载Tile定义并协调生成Til ...

  8. UI学习笔记---第九天UITableView表视图

    UITableView表视图 一.表视图的使用场景 表视图UITableView是iOS中最重要的视图,随处可见,通常用来管理一组具有相同数据结构的数据 表视图继承自UIScrollView,所以可以 ...

  9. VSTO学习笔记(十四)Excel数据透视表与PowerPivot

    原文:VSTO学习笔记(十四)Excel数据透视表与PowerPivot 近期公司内部在做一种通用查询报表,方便人力资源分析.统计数据.由于之前公司系统中有一个类似的查询使用Excel数据透视表完成的 ...

随机推荐

  1. jmeter之json数据参数化 断言等

    在 http Load Testing 中,json 数据的提交是个让人头疼的问题.本文详细介绍如何进行 JMeter 的 json 测试提交,以及如何将其参数化.Step 1 http json 请 ...

  2. JSONObject与JSONArray的使用

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  3. 【模拟题(电子科大MaxKU)】解题报告【树形问题】【矩阵乘法】【快速幂】【数论】

    目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s ...

  4. Linux-守护进程的实现

    Some basic rules to coding a daemon prevent unwanted interactions from happening. We state these rul ...

  5. POJ 1845 求a^b的约数和

    题目大意就是给定a和b,求a^b的约数和 f(n) = sigma(d) [d|n] 这个学过莫比乌斯反演之后很容易看出这是一个积性函数 那么f(a*b) = f(a)*f(b)  (gcd(a,b) ...

  6. GITHUB的初次使用

          对于一个从未用过  接触过github的人来说,达到一个最终的成功真可谓是历经千辛万苦.在这里真的感谢我们的小组组长,我遇到的问题除了自己的查到的,剩 下的基本上都是组长帮我解决的.当所有 ...

  7. Linux下备份系统至另一硬盘

    首先会想到dd命令. 但,, 1,若是小硬盘还好,上T的大硬盘这样做肯定不明智; 2,况且dd是在硬件层面的拷贝,前面的MBR也会随之恢复到另一个盘,若源硬盘是100G,目标盘是200G,又会出问题, ...

  8. 在Hadoop集群中添加机器和删除机器

    本文转自:http://www.cnblogs.com/gpcuster/archive/2011/04/12/2013411.html 无论是在Hadoop集群中添加机器和删除机器,都无需停机,整个 ...

  9. == 和 isEqualToString的区别之备忘

    == 比较的是指针 isEqualToString 比较的是指针指向的内容 比如: NSString * strA = @"abc"; NSString * strB = @&qu ...

  10. 有关WAMPSERVER 环境搭建 如何修改端口,MySQL数据库的修改

    环境搭建 http://share.weiyun.com/88896747fedd4e8b19afebea18f7684c 一.修改Apache的监听端口 1.在界面中选Apache,弹出隐藏菜单选项 ...