##DAY11 UITableView编辑

每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用

self.navigationItem.leftBarButtonItem = self.editButtonItem;

添加编辑:

[_tableView setEditing:YES animated:YES];

#pragma mark -------表视图移动操作---------

1、移动的第一步也是需要将表视图的编辑状态打开

setEditing:editing animated:

2、指定哪些行可以移动,默认都可以移动

-------UITableViewDataSource 协议的方法---------

tableView:canMoveRowAtIndexPath:

3、移动完成后要做什么事,怎么完成移动

-------UITableViewDataSource 协议的方法---------

tableView:moveRowAtIndexPath:toIndexPath:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

//先记录一下原有位置的模型数据

Student *student = _dataArray[sourceIndexPath.row];

//引用计数+1

[student retain];

//删除原位置下的模型数据

[_dataArray removeObjectAtIndex:sourceIndexPath.row];

//在新位置将记录的模型数据添加到数据数组中

[_dataArray insertObject:student atIndex:destinationIndexPath.row];

[student release];

}

#pragma mark -------删除、添加数据---------

1、让将要执行删除、添加操作的表视图处于编辑状态

setEditing:editing animated:

2、指定表视图中哪些行可以处于编辑状态,此方法如果不重写,默认所有的行可以进行编辑

-------UITableViewDataSource 协议的方法---------

tableView:canEditRowAtIndexPath:

3、指定编辑样式,到底是删除还是添加,此方法如果不重写,默认是删除样式,用 | 表示多选

-------UITableViewDelegate 协议的方法---------

tableView:editingStyleForRowAtIndexPath:

4、提交,不管是删除还是提交,这个方法才是核心方法,当点击删除、或者添加按钮时,需要做什么事情,怎样才能完成删除或者添加操作,全部都在这里指定,点击删除按钮或提交按钮触发

-------UITableViewDataSource 协议的方法---------

tableView:commitEditingStyle:forRowAtIndexPath:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

//1、表视图开始更新

[tableView beginUpdates];

//2、判断编辑样式

if(editingStyle == UITableViewCellEditingStyleDelete) {

//3、将该位置下的单元格删除

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

//4、删除数据数组中与该单元格绑定的数据

[_dataArray removeObjectAtIndex:indexPath.row];

}else if (editingStyle == UITableViewCellEditingStyleInsert) {

Student *student = _dataArray[indexPath.row];

//构建一个位置信息

NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];

[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];

[_dataArray insertObject:student atIndex:index.row];

}

//5、表视图结束更新

[tableView endUpdates];

}

##DAY11 UITableView编辑的更多相关文章

  1. iOS学习30之UITableView编辑

    1. UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事 ...

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

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

  3. UITableView编辑

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

  4. iOS学习之UITableView编辑

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

  5. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  6. UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  7. UITableView编辑模式大全解

    1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...

  8. iOS - UITableView 编辑(cell的插入, 删除, 移动)

    UITableView Cell的插入/删除 核心API Class : UITableView Delegate : UITableViewDataSource, UITableViewDelega ...

  9. UITableView编辑模式

    UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...

随机推荐

  1. Oracle Bills of Material and Engineering Application Program Interface (APIs)

    In this Document Goal   Solution   1. Sample Notes for BOM APIs   2. Datatypes used in these APIs   ...

  2. HTTP状态码(HTTP Status Code)【转】

    HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释:点击查看 1xx(临时响应 ...

  3. 如风一样,飞翔------Day37

    我一直都觉得自己是个反应没有那么敏锐的人,而不幸的是其实也是,所以,在菜鸟的头衔上,我不得不再背负上一个笨鸟的"光芒".我不苛求一飞冲天的传奇,却也有着不甘寂寞的激昂,我选择先飞, ...

  4. linux下创建用户并且限定用户主目录

    Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号 一方面可以帮助系统管理员对使用系统的用户进 ...

  5. 打造 通用的 支持多数据库 操作的 DBHelper

    闲来无事,写一个通用的直持多数据库的DBHelper,支持单连接批量执行SQL 因为用了TransactionScope所以请引用System.TransactionScope.dll 代码尚未测试, ...

  6. ios 中的构造方法(二)

    在之前有简单介绍了构造方法的结构,以及构造方法的作用,那么我们现在来讨论一下: 对象的创建分为两步:+ alloc 分配内存空间和 -init 进行初始化 那么在继承自 NSObject 的类当中,我 ...

  7. JS控制静态页面之间传递参数获取参数并应用

    在项目中遇到这也一个问题: 有a.html和b.html. 1.a页面已经打开,b页面尚未打开,我希望在a页面设置好一些列参数,比如背景色,宽度等参数,传递给b页面,好让b页面在打开就能应用. 2.a ...

  8. WTL CEdit关联绑定ID,滚动到最新的一行

    绑定控件 HWND logEdit = ::GetDlgItem(this->m_hWnd, IDC_EDIT_LOG); m_outputlogEdit.Attach(logEdit); 滚动 ...

  9. Ubuntu14.04(64位)安装ATI_Radeon_R7_M265显卡驱动

    电脑型号:Dell inspiron 14-5447 笔记本 显卡配置:集成显卡Intel核心显卡,Cpu是i5-4210U;独立显卡ATI_Radeon_R7_M265 网上关于ATI/Intel双 ...

  10. mysql学习(四)-字段类型

    mysql数据类型: 数值型: 整形:int 浮点型:float double decimal:定点型 日期: date  '2012-01-02' time '10:01:01' datetime ...