UITableView编辑

  1> UITableView 编辑流程

  2> UITableView 编辑步骤(四步)

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

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

  ② 协议设定

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

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

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

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

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

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

 3> 添加(前两步通用)

  第三步:  

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

  第四步:

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

 4> 删除(前两步通用)

  第三步:

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

  第四步:

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

 5>添加 和 删除 结合   

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

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

  第四步 :  

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

 6> 移动

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

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

  ② 移动

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

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

  1. 1 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
  2. 2 {
  3. 3 // sourceIndexPath 为原位置
  4. 4 // proposedDestinationIndexPath 为将要移动到的位置
  5. 5 if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
  6. 6 return proposedDestinationIndexPath;
  7. 7 } else {
  8. 8 return sourceIndexPath;
  9. 9 }
  10. 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. 第二篇:数据可视化 - 基本API

    前言 数据可视化是数据挖掘非常重要的一个环节,它不单在查阅了解数据环节使用到,在整个数据挖掘的流程中都会使用到. 因为数据可视化不单可以形象地展示数据,让你对数据有更好的总体上的了解,而且还可以让你清 ...

  2. 初学者第二节之HelloWorld

    ava具有简单性.面向对象.分布式.健壮性.安全性.平台独立与可移植性.多线程.动态性等特点.Java可以编写桌面应用程序.Web应用程序.分布式系统和嵌入式系统应用程序等. 一.首先创建一个Hell ...

  3. 【BZOJ2843】极地旅行社(Link-Cut Tree)

    [BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...

  4. 【BZOJ2818】Gcd(莫比乌斯反演)

    [BZOJ2818]Gcd(莫比乌斯反演) 题面 Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Ou ...

  5. mysql的conv的用法

    这次的ctf比赛用到这个函数,这里记录一下 题目禁了ascii , ord 那就使用conv 这个函数是用来将字符转换进制的,例如将a转成ASCII码(换个说法就是将16进制的a换成10进制) 那就直 ...

  6. TP5模型关联问题

    在使用模型关联时:假如有表   merchant商户表   m_store 店铺表  m_store_ref 商户店铺关联表  user 普通用户表 $mer = Merchant::with([ ' ...

  7. Android端 高德地图点击得到经纬度

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  8. 解决linux安装软件:/lib/ld-linux.so.2: bad ELF interpreter问题

    问题:64位系统中安装了32位程序解决办法 是因为64位系统中安装了32位程序 解决方法: yum install glibc.i686

  9. Mac上使用jenkins+ant执行第一个程序

    本文旨在让同学们明白如何让jenkis在mac笔记本上运行,以模拟实际工作中在linux上搭建jenkins服务平台首先按照笔者的习惯先说一下如何安装jenkis和tomcat,先安装tomcat,在 ...

  10. 初识Selenium以及Selenium常用工具的简单介绍

    一.为什么要学习自动化测试? 在互联网行业中敏捷开发小步快跑,快速迭代,测试环节中回归测试任务大繁琐,手工测试容易漏测,自动化测试可以提高测试效率保证产品质量. 二.学习的层次模型 1.单元自动化测试 ...