##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. 如何同时激活两个不同版本的MyEclipse 【MyEclipse2013和MyEclipse2014同时激活】

    激活一个MyEclipse的步骤,大家都会,在这里就不多说了,不会的可以看:http://jingyan.baidu.com/article/3ea51489cc14d452e71bba7a.html ...

  2. 网易云课堂_程序设计入门-C语言_第二周:判断_1时间换算

    1 时间换算(5分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果 ...

  3. localStorage点击次数存储

    <!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教 ...

  4. 一,入门基础—— 2. 第一个project项目

    1. 欢迎界面的右边是一个项目列表,显示全部近期打开的项目,双击⭕️打开之前创建的项目. 2.右击⭕️处,选择"Add Files to DemoApp..."加入一张图片. 3. ...

  5. Swfit中视图跳转

    .跳转到任一UIViewController var sb = UIStoryboard(name: "Main", bundle:nil) var vc = sb.instant ...

  6. DevExpress控件之:ChartControl 动态绑定数据

    private void BindData(ViewType vt) { chartControl1.Series.Clear(); //Series series1 = new Series(&qu ...

  7. JS 事件绑定的几种方式 小笔记

    第一种 var test=document.getElementById('add'); add.onclick=function(){ alert('1'); } 直接在对象上注册事件 缺点:如果我 ...

  8. Angular-UI-Router 学习笔记

    路由 Route 我在 慕课网 学习 AngularJS 为什么用 Route AJAX 请求不会留下 History 记录 用户无法直接通过 URL 进入应用中的指定页面(保存书签.链接分享给朋友) ...

  9. GCD 的初步认识

    1.什么是 GCD? GCD为Grand Central Dispatch的缩写 (GCD)是Apple开发的一个多核编程的较新的解决方法.它主要用于优化应用程序以支持多核处理器以及其他对称多处理系统 ...

  10. 移动并改变alpha

    <script type="text/javascript">function obj(x){return document.getElementById(x);}va ...