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

//1、让将要执行删除、添加操作的表视图处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//先执行父类中的这个方法
[super setEditing:editing animated:animated];
//表视图执行此方法
[self.tableView setEditing:editing animated:animated];
}
//2、指定表视图中哪些行可以处于编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// if (indexPath.row % 2 == 0) {
// return YES;
// }
// return NO;
//默认全部都可以进行编辑
return YES;
}
//3、指定编辑样式,到底是删除还是添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//如果此方法不重写,默认是删除样式
return UITableViewCellEditingStyleInsert;
// return UITableViewCellEditingStyleDelete;
// if (indexPath.row > 10) {
// return UITableViewCellEditingStyleInsert;
// } else {
// return UITableViewCellEditingStyleDelete;
// }
}
//4、不管是删除还是添加,这个方法才是操作的核心方法,当点击删除、或者添加按钮时,需要做什么事情,怎样才能完成删除或者添加操作,全部在这个方法内部指定
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];//表视图开始更新
if (editingStyle == UITableViewCellEditingStyleDelete) {
//1、将该位置下的单元格删除
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//2、删除数据数组中,与该单元格绑定的数据
[_dataArray removeObjectAtIndex:indexPath.row];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
Student *student = _dataArray[indexPath.row];
//获取一个位置信息
NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
[_dataArray insertObject:student atIndex:index.row];
}
[tableView endUpdates];//表视图结束更新
//1、移动的第一步也是需要将表视图的编辑状态打开(上面已写)
//2、指定哪些行可以进行移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
//默认是都可以移动
return YES;
}
//3、移动完成之后要做什么事情,怎么完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//先记录原有位置下的模型数据
Student *student = _dataArray[sourceIndexPath.row];
[student retain];//防止野指针
//删除原位置下的模型数据
[_dataArray removeObjectAtIndex:sourceIndexPath.row];
//在新位置将记录的模型数据添加到数据组中
[_dataArray insertObject:student atIndex:destinationIndexPath.row];
//点击单元格触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//push操作、传值
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.student = _dataArray[indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
1、表视图控制器自带的根视图为UITableView对象
2、self.tableView就代表根视图
3、创建出来的表视图控制器 已经 自动接收了 数据源协议和代理协议
4、不需要通过代码建立协议与代理关系
5、表视图控制器.m文件已经自动帮你添加了数据源协议中必须实现的方法
UITableView编辑的更多相关文章
- iOS学习30之UITableView编辑
1. UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事 ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- iOS学习之UITableView编辑
一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...
- ##DAY11 UITableView编辑
##DAY11 UITableView编辑 每一个视图控制器都有一个编辑按钮,因为项目中编辑的应用场景非常多,所以系统预留了一个编辑按钮供我们使用 self.navigationItem.leftBa ...
- UI:UITableView 编辑、cell重用机制
tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...
- UITableView 编辑模式(增加-删除-移动---自定义左滑 title)
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- UITableView编辑模式大全解
1.UITableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性 tableView?.editing = true // 这个设置的时候是有动画效果的 tableVi ...
- iOS - UITableView 编辑(cell的插入, 删除, 移动)
UITableView Cell的插入/删除 核心API Class : UITableView Delegate : UITableViewDataSource, UITableViewDelega ...
- UITableView编辑模式
UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...
随机推荐
- java开发_读写txt文件操作
package com.mi.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStr ...
- 《Unix网络编程》卷一(简介TCP/IP、基础套接字编程)
通常说函数返回某个错误值,实际上是函数返回值为-1,而全局变量errno被置为指定的常值(即称函数返回这个错误值). exit终止进程,Unix在一个进程终止时总是关闭该进程所有打开的描述符. TCP ...
- oracle和mysql获取当前时间的不同
在oracle中:select sysdate from dual,而且必须加上from dual,否则报错,只支持sysdate ,和mysql不一样,mysql的是sysdate()函数: 在my ...
- jquery中没有innerHTML
本人正在学习使用jQuery. 发现如果我在div或者其他非表单的标签中赋值,原本用普通的js就直接document.getElementById("id").innerHtml( ...
- springmvc web-info目录下无法引入的js文件无效
今天在联系spring的时候而然遇到了个不起眼的问题.那就是在html或者说jsp页面中引用js文件的时候总是提示找不到路径.eclipse更是抛出 No mapping to aa.js. 我就奇怪 ...
- 如何从github上面拷贝源码
有好奇心的朋友们一定都想看一看很多开源项目的源码,那么github就不用说啦,太多的开源项目都把源码放到上面. 博主最近为了学习angularjs也不得不去github上面弄源码,下面将会介绍如何做: ...
- Animation小问题整理
1.在动画播放中改变层级内容的名字,不会造成动画内容映射的改变. 2.Animator动画剪辑层级没问题,但是不播放 检查是否Mask损坏,FBX文件的Animations选项下面有个Mask.Uni ...
- Duilib实现QQ聊天窗口晃动
转载:http://blog.csdn.net/arbboter/article/details/26282717 转载:http://blog.csdn.net/zerolusta/article/ ...
- [问题2015S07] 复旦高等代数 II(14级)每周一题(第八教学周)
[问题2015S07] 设 \(A\) 为 \(n\) 阶复方阵, 证明: 存在 \(n\) 阶非异复对称阵 \(S\), 使得 \(A'=S^{-1}AS\), 即 \(A\) 可通过非异复对称阵 ...
- [问题2014A03] 解答
[问题2014A03] 解答 注意到 \((A^*)^*\) 的第 (1,1) 元素是 \(A^*\) 的第 (1,1) 元素的代数余子式, 即为 \[\begin{vmatrix} A_{22} ...