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 ...
随机推荐
- 经验总结:HTTP返回505错误小记
昨天调试代码的时候,用http请求一个图片,返回的结果为如下: HTTP/1.1 505 HTTP Version Not Supported Server: Apache-Coyote/1.1 Da ...
- app令牌的一个token实现
app登陆验证不能使用session来判断了.然后查资料都说用令牌,没找到合适的方法,我的眼界太小.另外,越来越感觉基础的重要,比如,session是什么,我竟无言以对.不知道session是什么,怎 ...
- 通过自定义相册来介绍photo library的使用
因为我在模仿美图秀秀的功能,在使用相册时候,UIImagePickerController本来就是一个UINavigationController的子类,所以没有办法使用push,所以做了一个自定义的 ...
- React-Native 给客户端来个「同音词模糊搜索」
APP上线一段时间有用户反应说不方便找东西,其实APP的数据不大也就三四百条而以,但受限于手机端展示区域太小.信息大爆炸,大家也基本上不会去记具体的名称都是根据模糊的印象进行搜索而且现在大家基本都用拼 ...
- Razor练习2
Razor的数据类型有string,int,float,decimal,bool等. 另外需要对数据类型的转换,通常的方法有如下:ToString(): 转换数据类型为字符串(string).此与C# ...
- 如何获取配置文件Web.config的AppSetting节点数据
ConfigurationManager必须要先在工程里添加system.configuration.dll程序集的引用. 1 System.Configuration.ConfigurationMa ...
- 走进异步世界-犯傻也值得分享:ConfigureAwait(false)使用经验分享
在上周解决“博客程序异步化改造之后遭遇的性能问题”的过程中,我们干了一件自以为很有成就感的事——在表现层(MVC与WebForms)将所有使用await的地方都加上了ConfigureAwait(fa ...
- 【Java每日一题】20161122
package Nov2016; import java.util.ArrayList; import java.util.Iterator; public class Ques1122 { publ ...
- ASP.NET MVC进阶三
一.ASP.NET MVC中的AJAX应用 首先,在ASP.NET MVC中使用自带的ajax功能,必须要导入2个js文件(顺序不能颠倒): ASP.NET MVC提供了2个常用的ajax辅助方法. ...
- Scalaz(50)- scalaz-stream: 安全的无穷运算-running infinite stream freely
scalaz-stream支持无穷数据流(infinite stream),这本身是它强大的功能之一,试想有多少系统需要通过无穷运算才能得以实现.这是因为外界的输入是不可预料的,对于系统本身就是无穷的 ...