UITableView增加和删除、移动
复习一下:
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增加和删除、移动的更多相关文章
- UITableView划动删除的实现
对于app应用来说,使用列表的形式展现数据非UITableView莫属.在熟练掌握了用UITableView展示数据以后,是不是也遇到了需要删除数据的需求?是不是觉得在一行数据上划动一下,然后出现一个 ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- iOS --SQL的增加、删除、查找、修改
iOS对于数据库的操作:增加.删除.查找.修改 首先需要创建一个数据库:本程序的数据库是在火狐浏览器里的插件里写的微量型数据库 火狐找查找SQLite Manager的步骤: 第一步:在工具栏找到附加 ...
- 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常
在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...
- 使用AutoIT对增加和删除文件属性的实现
编写历程: 前段日子,晚上下班回家,一个舍友问我可不可以将一个目录下的隐藏文件全部显示出来(变成非隐藏文件),我说可以. 之后就开始大刀阔斧的寻找方法来做这件事,上网找,说需要一个Windows下的小 ...
- 使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空
使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空 添加 alter table table_name add field_name field_type; 添加,并设置默认值,及非 ...
- Oracle表字段的增加、删除、修改和重命名
本文主要是关于Oracle数据库表中字段的增加.删除.修改和重命名的操作. 增加字段语法:alter table tablename add (column datatype [default val ...
- 10月16日下午MySQL数据库CRUD操作(增加、删除、修改、查询)
1.MySQL注释语法--,# 2.2.后缀是.sql的文件是数据库查询文件. 3.保存查询. 关闭查询时会弹出提示是否保存,保存的是这段文字,不是表格(只要是执行成功了表格已经建立了).保存以后下次 ...
- Oracle 增加修改删除字段与添加注释
添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….); 修改字段的语法:alter ...
随机推荐
- placeholder的兼容处理(jQuery下)
这是一个老问题,结合前辈们的经验,需要处理的问题有一下几个. 1.只有输入框(input/textarea)下的palaceholder属性存在的时候才需要处理这类兼容 2.处理好输入框上焦点和是焦点 ...
- postgres中的中文分词zhparser
postgres中的中文分词zhparser postgres中的中文分词方法 基本查了下网络,postgres的中文分词大概有两种方法: Bamboo zhparser 其中的Bamboo安装和使用 ...
- 能不能用javascript实现素数求和问题呢?
先自己试试吧 好吧,下面这段代码用了别人所说的最笨的方法,身为小白的我只能呵呵.待会再尝试用其他算法. <!DOCTYPE html> <html lang="en&quo ...
- LINQ to SQL语句(2)之Select/Distinct
适用场景:o(∩_∩)o- 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也就是结果返回回来:延迟.Select/ ...
- The SQL Server Service Broker for the current database is not enabled, and as a result query notifications are not supported.
当Insus.NET尝试解决此问题<When using SqlDependency without providing an options value, SqlDependency.Star ...
- 用纯css画个三角形
用纯css画个三角形以下是源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- Ajax实现定时刷新页面
function deleteValue(){ var refresh = function() { $.ajax({ type:'post', url:'/Application/index ...
- Asp.net Mvc模块化开发系列(目录)
模块化开发是非常重要的,模块化开发是个系统性问题,为此我觉得有必须要写一个系列的文章才能基本说的清楚 那又为什么要写一个目录呢? 其一.是对我昨天承诺写一个系列新的文章的回应 其二.是先写出一个大纲, ...
- cefglue埋坑记录
很少写博客,写的不好,请多多包含,主要是记录工作中的一些问题,和园子里朋友一起讨论学习. 写埋坑记录之前,我先介绍下为什么会使用这个webkit内核的浏览器组件,我是wpf项目使用富文本编辑器,话说w ...
- .NET使用ZXing.NET生成中间带图片的二维码
很久之前就有写这样的代码了,只是一直没记录下来,偶然想写成博客. 把之前的代码封装成函数,以方便理解以及调用. 基于开源的 ZXing.NET 组件,代码如下: 先添加对ZXing.NET的引用,然后 ...