复习一下:

1、在控制器上添加一个UITableView,  暂时该UITableView控件变量名命名为为tableView, 设置控件代理,实现控制器的UITableViewDataSource, UITableViewDelegate协议;

2、tableView控件的editing属性默认是NO, 并且UITableViewCell默认情况下没有删除和增加功能。

实现代理方法

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

  后,然后UITableViewCell向左拖拽时会出现删除按钮:

在代理方法里面做相应处理,就可以实现删除功能,代码如下:

//代理方法,实现后可以进行增加单元行或者删除单元行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
// UITableViewCellEditingStyleNone,
// UITableViewCellEditingStyleDelete, //表示删除
// UITableViewCellEditingStyleInsert //表示增加
// };
// NSLog(@"%d", editingStyle);
//当样式是删除操作,进行删除
if (editingStyle == UITableViewCellEditingStyleDelete){ //删除数组中一行
[self.arrays[indexPath.section] removeObjectAtIndex:indexPath.row]; // [tableView reloadData]; //删除后全部重新加载
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];//只刷新删除行部分(性能更好一些)
} }

  

这里还有一个代理方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;当没有实现此方法时,默认是返回

UITableViewCellEditingStyleDelete枚举,要想实现单元格增加,就要实现此方法,并且返回UITableViewCellEditingStyleInsert枚举

然后还要设置tableView控件属性 editing 为YES, 完整代码如下:

- (void)viewDidLoad {
[super viewDidLoad]; //...... //实现代码 //设置tableView控件editing属性
tableView.editing = YES; //设置可编辑
} //delegate代理方法,实现此方法,可以设置UITableViewCell增加或删除功能,如果不实现此方法,默认都是删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row % 2 == 0){
return UITableViewCellEditingStyleInsert;
}
else{
return UITableViewCellEditingStyleDelete;
}
} //处理UITableViewCell的增加和删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
// UITableViewCellEditingStyleNone,
// UITableViewCellEditingStyleDelete,
// UITableViewCellEditingStyleInsert
// }; //删除操作
if (editingStyle == UITableViewCellEditingStyleDelete){ [self.arrays[indexPath.section] removeObjectAtIndex:indexPath.row]; // [tableView reloadData]; //全部重新加载
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];//删除行
}//插入操作
else if (editingStyle == UITableViewCellEditingStyleInsert){
TanPerson *per = [[TanPerson alloc] init];
per.name = @"哈哈哈哈";
per.address = @"小楼昨夜又东风";
[self.arrays[indexPath.section] insertObject:per atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}

 运行后一加载截图为:

   可以进行增加或删除操作:

3、UITableViewCell的移动:实现一个代理方法,就可以进行单元格的移动:

//实现此方法,就可以移动单元格, 方法里面是让数据和样式移动保持一致
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
// NSLog(@"移动了”");
TanPerson *sourcePerson = self.arrays[sourceIndexPath.section][sourceIndexPath.row];
TanPerson *destPerson = self.arrays[destinationIndexPath.section][destinationIndexPath.row]; self.arrays[destinationIndexPath.section][destinationIndexPath.row] = sourcePerson;
self.arrays[sourceIndexPath.section][sourceIndexPath.section] = destPerson;
}

  按住想要移动的UITableViewCell的哪个三横图标,可以进行移动

UITableView增加和删除、移动的更多相关文章

  1. UITableView划动删除的实现

    对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个 ...

  2. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  3. iOS --SQL的增加、删除、查找、修改

    iOS对于数据库的操作:增加.删除.查找.修改 首先需要创建一个数据库:本程序的数据库是在火狐浏览器里的插件里写的微量型数据库 火狐找查找SQLite Manager的步骤: 第一步:在工具栏找到附加 ...

  4. 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常

    在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...

  5. 使用AutoIT对增加和删除文件属性的实现

    编写历程: 前段日子,晚上下班回家,一个舍友问我可不可以将一个目录下的隐藏文件全部显示出来(变成非隐藏文件),我说可以. 之后就开始大刀阔斧的寻找方法来做这件事,上网找,说需要一个Windows下的小 ...

  6. 使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空

    使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空 添加 alter table table_name add field_name field_type; 添加,并设置默认值,及非 ...

  7. Oracle表字段的增加、删除、修改和重命名

    本文主要是关于Oracle数据库表中字段的增加.删除.修改和重命名的操作. 增加字段语法:alter table tablename add (column datatype [default val ...

  8. 10月16日下午MySQL数据库CRUD操作(增加、删除、修改、查询)

    1.MySQL注释语法--,# 2.2.后缀是.sql的文件是数据库查询文件. 3.保存查询. 关闭查询时会弹出提示是否保存,保存的是这段文字,不是表格(只要是执行成功了表格已经建立了).保存以后下次 ...

  9. Oracle 增加修改删除字段与添加注释

    添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….); 修改字段的语法:alter ...

随机推荐

  1. Java判断回文数算法简单实现

    好久没写java的代码了, 今天闲来无事写段java的代码,算是为新的一年磨磨刀,开个头,算法是Java判断回文数算法简单实现,基本思想是利用字符串对应位置比较,如果所有可能位置都满足要求,则输入的是 ...

  2. SQL Server代理(7/12):作业活动监视器

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 在这个系列的前几篇文章里,你创建配置了SQ ...

  3. Web API应用架构在Winform混合框架中的应用(2)--自定义异常结果的处理

    在上篇随笔<Web API应用架构在Winform混合框架中的应用(1)>中我介绍了关于如何在Winfrom里面整合WebAPI,作为一个新型数据源的接入方式,从而形成了三种不同的数据提供 ...

  4. 这几天做完简易酒店管理系统,对Sql Server执行计划的浅显了解。

    我是一名大三的小学生,今天开始我的第一篇博客,最近随便做了一个简易的酒店管理系统,对sql执行计划有了初步的了解. 查看上面语句的预估执行计划,在工具栏中有这个按钮 聚集索引扫描被称为Index Sc ...

  5. IIS8发布WCF接口中遇到的问题总结

    环境:系统Win8.1+VS2013+IIS8 在VS13中新建一个WCF服务应用程序,新建之后系统会默认给我们新建一个方法.

  6. iOS取得AddressBook联系人信息

    新建一个CContact类用于存放联系人信息,下面是该类的代码: CContact.h代码: 01 #import <Foundation/Foundation.h> 02   03   ...

  7. Structs2动态方法调用

    第一种:指定Method属性(Action比较多) <!-- 声明包 --> <package name="user" extends="struts- ...

  8. 2016 年青岛网络赛---Tea

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5881 Problem Description Tea is good. Tea is life. Te ...

  9. ThinkCMF变量输出+使用函数

    ThinkCMF变量输出+使用函数的方式同ThinkPHP. ThinkPHP变量输出: 在模板中输出变量的方法很简单,例如,在控制器中我们给模板变量赋值: $name = 'ThinkPHP'; $ ...

  10. js中的原型、继承的一些想法

    最近看到一个别人写的js类库,突然对js中的原型及继承产生了一些想法,之前也看过其中的一些内容,但是总不是很清晰,这几天利用空闲时间,对这块理解了一下,感觉还是有不通之处,思路上没那么条理,仅作为分享 ...