1. UITableView编辑

  1> UITableView 编辑流程

  2> UITableView 编辑步骤(四步)

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

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

  ② 协议设定

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

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

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

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

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

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

 3> 添加(前两步通用)

  第三步:  

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

  第四步:

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

 4> 删除(前两步通用)

  第三步:

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

  第四步:

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

 5>添加 和 删除 结合   

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

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

  第四步 :  

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

 6> 移动

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

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

  ② 移动

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

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

 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// sourceIndexPath 为原位置
// proposedDestinationIndexPath 为将要移动到的位置
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
}

2. UITableViewController

 1> 概述

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

 2> 注意事项

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

iOS学习30之UITableView编辑的更多相关文章

  1. iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决

    最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...

  2. iOS学习之UITableView编辑

    一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...

  3. iOS学习之UITableView中Cell的操作

    接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...

  4. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  5. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

  6. iOS Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:7962

      Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Cac ...

  7. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  8. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

  9. 2015最新iOS学习线路图

    iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...

随机推荐

  1. 比较 http连接 vs socket连接

    http连接 :短连接,客户端,服务器三次握手建立连接,服务器响应返回信息,连接关闭,一次性的socket连接:长连接,客户端,服务器三次握手建立连接不中断(通过ip地址端口号定位进程)及时通讯,客户 ...

  2. Git - 使用指南

    GIT (分布式版本控制系统) 编辑 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.[1]  Git的读音为/gɪt/. Git是一个开源的分布式版本控制系统,可以 ...

  3. DNS原理

    DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...

  4. app性能测试点、安全测试点总结

  5. C#学习笔记----C#中的闭包机制

    http://www.cnblogs.com/jiejie_peng/p/3701070.html http://www.cnblogs.com/Ribbon/p/3611457.html “ 若匿名 ...

  6. Oracle里SID、SERVICE_NAME

    本文仅用作备忘,无实际指导意义,逻辑略混乱. 1.命令show parameter name; SQL> show parameter name; NAME TYPE VALUE ------- ...

  7. 攻城狮在路上(叁)Linux(十二)--- Linux的目录与路径

    一.相对路径与绝对路径: A.绝对路径:由根目录/开始写起的路径,例如 /usr/share/doc B.相对路径:不是由根目录/开始写起的路径. 二.目录的相关操作: 1.cd: 目录切换 cd ~ ...

  8. js 随机星星 document.createElement(); setAttribute()

    js 随机星星 document.createElement(); setAttribute() <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. ...

  9. 三、spinner

    今天 ,看的和学的都不多,就弄了一个spinner控件而已,下面就记录一下spinner 用法吧 基本上说,使用spinner 有三个步骤 一.在布局文件里面设置spinner 控件,这个不用再多说了 ...

  10. 跟着鸟哥学Linux系列笔记1

    跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 装完linux之后,接下来一步就是进行相关命令的学习了 第五章:首次登录与在线求助man page 1. X ...