UITableView编辑

  1> UITableView 编辑流程

  2> UITableView 编辑步骤(四步)

  ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法中) 

1     // 优化写法
2 // 不带动画
3 _rootView.tableView.editing = !_rootView.tableView.editing;
4 // 带动画
5 [_rootView.tableView setEditing:!_rootView.tableView.editing animated:YES];

  ② 协议设定

   第二步 : 确定cell是否处于编辑状态(UITableViewDataSource协议的方法) 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 所有的cell都可以进行编辑时,整个方法可以省略
// return YES; // 只有第一个分区可以被编辑
if (0 == indexPath.section) {
return YES;
}
return NO;
}

   第三步 : 设定cell的编辑样式 (删除 , 添加)

1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 return UITableViewCellEditingStyle枚举中的样式;
4 }

   第四步 : 编辑状态进行添加

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 // 1. 处理数据
4 // 2. 更新UI界面
5 }

 3> 添加(前两步通用)

  第三步:  

1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 return UITableViewCellEditingStyleDelete;
4 }

  第四步:

 1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 // 1. 删除数据
4 [_allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
5
6 // 2. 更新UI
7 // 单独更新一行(删除)
8 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
9
10 // 全部更新
11 // [tableView reloadData];
12 }

 4> 删除(前两步通用)

  第三步:

1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 return UITableViewCellEditingStyleInsert;
4 }

  第四步:

 1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 // 1. 插入数据到数组中
4 [_allDataArray[indexPath.section] insertObject:@"你是不是傻" atIndex:indexPath.row + 1];
5
6 // 2. 更新UI
7 // 全部更新
8 // [tableView reloadData];
9
10 // 单独更新一行
11
12 // 创建新一行的NSIndexPath对象
13 NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
14
15 [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
16 }

 5>添加 和 删除 结合   

  第三步 : 设置一个 UITableViewCellEditingStyle 类型的 属性(style) 用于存储 添加 和 删除 的编辑的样式,
在 添加 和 删除 对应的点击事件方法中赋值, 注:按钮的功能样式需要在点击事件最上面实现, 否则会出现bug

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.style;
}

  第四步 :  

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3 // 判断编辑样式
4 if (UITableViewCellEditingStyleDelete == editingStyle) {
5 删除操作,详情请见 3> 第四步
6 } else if (UITableViewCellEditingStyleInsert == editingStyle){
7 添加操作,详情请见 4> 第四步
8 }
9 }

 6> 移动

  ① (在 TableView 处于编辑状态下)实现协议: 告诉 tableView 是否能够移动 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

  ② 移动

 1 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
2 {
3 // 1. 获取需要修改的数据
4 NSString *sourceData = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
5
6 // 2. 先将数据从当前的位置移除
7 [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
8
9 // 3. 将数据插入到对应的位置
10 [self.allDataArray[destinationIndexPath.section] insertObject:sourceData atIndex:destinationIndexPath.row];
11 }

  bug修正--- 防止跨分区移动

 1 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
2 {
3 // sourceIndexPath 为原位置
4 // proposedDestinationIndexPath 为将要移动到的位置
5 if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
6 return proposedDestinationIndexPath;
7 } else {
8 return sourceIndexPath;
9 }
10 }

UITableViewController

 1> 概述

  UITableViewController 是继承于 UIViewController 中的一个类,只不过比UIViewController 中多了一个属性 tableView 。
即: UITableViewController 是一个自带 table 的视图控制器。

 2> 注意事项

  • UITableViewController 继承 UIViewController , 自带一个tableView
  • self.view 不是 UIView  是 UITableView
  • datasource 和 delegate 默认都是 self (UITableViewController)
  • 开发中只需要建 UITableViewController 子类

iOS开发-OC中TabView的编辑的更多相关文章

  1. iOS开发OC基础:Xcode中常见英文总结,OC常见英文错误

    在开发的过程中难免会遇到很多的错误,可是当看到系统给出的英文时,又不知道是什么意思.所以这篇文章总结了Xcode中常见的一些英文单词及词组,可以帮助初学的人快速了解给出的提示.多练习,就肯定能基本掌握 ...

  2. iOS开发 Xcode8中遇到的问题及改动

      iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...

  3. iOS开发——基础篇——iOS开发 Xcode8中遇到的问题及改动

      iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...

  4. iOS开发——OC篇&OC高级语法

    iOS开发高级语法之分类,拓展,协议,代码块详解 一:分类 什么是分类Category? 分类就是类的补充和扩展部分 补充和扩展的每个部分就是分类 分类本质上是类的一部分 分类的定义 分类也是以代码的 ...

  5. iOS开发——OC篇&纯代码退出键盘

    关于iOS开发中键盘的退出,其实方法有很多中,而且笔者也也学会了不少,包括各种非纯代码界面的退出. 但是最近开始着手项目的时候却闷了,因为太多了,笔者确实知道有很多中方法能实现,而且令我影响最深的就是 ...

  6. iOS开发-OC语言 (一)oc数据类型

    分享一套以前学习iOS开发时学习整理的资料,后面整套持续更新: oc数据类型 数据类型:基本数据类型.指针数据类型 基本数据类型:数值型.字符型(char).布尔型.空类型(void) 指针数据类型: ...

  7. iOS开发——OC篇&常用关键字的使用与区别

    copy,assign,strong,retain,weak,readonly,readwrite,nonatomic,atomic,unsafe_unretained的使用与区别 最近在学习iOS的 ...

  8. iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)

     iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action)   今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...

  9. iOS开发-OC数据类型

    以下是OC中的实例,Swift部分不适用 iOS中的注释 // 单行注释 // 注释对代码起到解释说明的作用,注释是给程序员看的,不参与程序运行 /*  多行注释   Xcode快捷键   全选 cm ...

随机推荐

  1. RobotFramework下的http接口自动化Set Request Body 关键字的使用

    Set Request Body关键字用来设置http 请求时的body 信息,尤其是在post 请求时,经常需要用到这个关键字. 该关键字接收一个参数,[ body ] 示例1:登录博客园(http ...

  2. Dynamics 365中审核用户权限变化的一种方法

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复268或者20180311可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  3. 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)

    [UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...

  4. 回滚 - 每天5分钟玩转 Docker 容器技术(141)

    kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision. 默认配置下,Kubernet ...

  5. C++学习-6

    1.Auto无法区分常量变量,引用常量(顶层const被忽略了),不能识别引用变量,const和&都无法识别 Auto不能放在结构体内部 2.decltype()能识别引用,能获取常量属性,t ...

  6. 题目1010:A + B

    题目描述: 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. 输入: 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B ...

  7. MySQL多数据源笔记5-ShardingJDBC实战

    Sharding-JDBC集分库分表.读写分离.分布式主键.柔性事务和数据治理与一身,提供一站式的解决分布式关系型数据库的解决方案. 从2.x版本开始,Sharding-JDBC正式将包名.Maven ...

  8. 深度揭秘腾讯云TSF日调用量超万亿次背后技术架构

    腾讯云TSF是整合外部开源框架和腾讯内部历经多年锤炼的PaaS平台打造而成的企业级分布式应用服务开发与托管平台,本文重点对TSF中负责服务托管的PaaS平台进行揭秘,从技术角度解析TSF 平台是如何每 ...

  9. 关于移动端的UI事件分类

    1. click事件 单击事件,类似于PC端的click,但在移动端中,连续click的触发有200ms ~ 300ms的延迟 2. touch类事件 触摸事件,有touchstart touchmo ...

  10. java基础学习系列二

    循环语句 1,for(){} 2,while(){} 3,do{}while() continue和break用法 break是结束循环 continue结束本次循环