TableViewController的添加,删除,移动
#import "RootTableViewController.h"
@interface RootTableViewController ()
{
UITableViewCellEditingStyle _style;
}
@property(nonatomic,strong)NSMutableArray *array;
@end
@implementation RootTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.array=[NSMutableArray array];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//在数组里存一些数据
for (int i=0; i<3; i++) {
NSMutableArray *tempArray=[NSMutableArray array];
for (int j=0; j<5; j++) {
[tempArray addObject:[NSString stringWithFormat:@"第%d组,第%d个人",i,j]];
}
[self.array addObject:tempArray];
}
// 设置代理
self.tableView.dataSource=self;
self.tableView.delegate=self;
//在导航栏两侧添加两个barbutton
UIBarButtonItem *bar=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction:)];
self.navigationItem.leftBarButtonItem=bar;
UIBarButtonItem *bar1=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction1:)];
self.navigationItem.rightBarButtonItem=bar1;
}
//barbutton编辑事件
-(void)barAction:(UIBarButtonItem *)sender
{
_style=UITableViewCellEditingStyleDelete;
BOOL flag=self.tableView.editing;
[self.tableView setEditing:!flag animated:YES];
}
//barbutton添加事件
-(void)barAction1:(UIBarButtonItem *)sender
{
_style=UITableViewCellEditingStyleInsert;
BOOL flag=self.tableView.editing;
[self.tableView setEditing:!flag animated:YES];
}
//设置哪些行能编辑(默认全部都能)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//确定编辑状态(删除|添加)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _style;
}
//提交编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle==UITableViewCellEditingStyleDelete)
{
//删除
[self.array[indexPath.section]removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}
else
{
//添加
NSString *s=@"测试数据";
[self.array[indexPath.section]insertObject:s atIndex:indexPath.row+1];
NSIndexPath *index=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationRight)];
}
}
//哪些行能移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//完成移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//先赋值
id obj=self.array[sourceIndexPath.section][sourceIndexPath.row];
//再删除
[self.array[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
//最后添加
[self.array[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.section];
}
//检查越界
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if(sourceIndexPath.section!=proposedDestinationIndexPath.section)
{
return sourceIndexPath;
}
else
{
return proposedDestinationIndexPath;
}
}
TableViewController的添加,删除,移动的更多相关文章
- WPF下的Richtextbox中实现表格合并,添加删除行列等功能
.Net中已有现在的方法实现这些功能,不过可能是由于未完善,未把方法公开出来.只能用反射的方法去调用它. 详细信息可以查看.Net Framework 的源代码 http://referencesou ...
- 编辑 Ext 表格(一)——— 动态添加删除行列
一.动态增删行 在 ext 表格中,动态添加行主要和表格绑定的 store 有关, 通过对 store 数据集进行添加或删除,就能实现表格行的动态添加删除. (1) 动态添加表格的行 gridS ...
- Angular-表单动态添加删除
angular本身不允许去操作DOM,在angular的角度来说,所有操作都以数据为核心,剩下的事情由angular来完成.所以说,想清楚问题的根源,解决起来也不是那么困难. 前提 那么,要做的这个添 ...
- 用Javascript动态添加删除HTML元素实例 (转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [CentOS]添加删除用户
摘要 在安装CentOS的时候,我们只设置了root,类似windows的超级管理员.当然我们在工作的时候,为了安全考虑,不可能对外开发root,一方面是从安全的角度,另一方面也是方便管理. 添加删除 ...
- iOS仿网易新闻栏目拖动重排添加删除效果
仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...
- 百度地图API示例之添加/删除工具条、比例尺控件
代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...
- Android 动态添加删除ExpandableListView的item的例子
这个例子可以学习到如下几点: 1.通过自定义Dialog(单独布局的xml文件进行弹出显示) 2.通过menu点击监听添加,删除view中的items 3.点击ExpandableListView中g ...
- jQuery添加删除元素
$(document).ready(function () { $('#radioExtranet').on('click', function () { showProjectInformation ...
随机推荐
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- CentOS Apache服务器安装与配置
原文地址:http://www.linuxidc.com/Linux/2014-01/95256.htm 一.安装Apache程序,一般有三种安装方式: Apache在centos下httpd1.直接 ...
- VB.NET 结构(Structure)和类(Class)的区别
类是我们用来构造 VB.NET 应用程序时的最基本的编程结构了. 那结构与类有什么相似之处与不同之处呢? 结构和类, 相同之处是都含有成员,包括构造函数.方法.属性.字段.常量.枚举和事件,都可以实现 ...
- 深入理解JavaWeb技术内幕之中文编码
为什么要编码 1.计算机中存储信息的最小单元是1个字节,所能表示的字符范围为0~255个. 2.人类要表示的符号太多,无法用1个字节来完全表示. 常见的编码类型 ASCII码 总共128个,用1个字节 ...
- 局域网内IP冲突怎么办
对于在Internet和Intranet网络上,使用TCP/IP协议时每台主机必须具有独立的IP地址,有了IP地址的主机才能与网络上的其它主机进行通讯.但IP地址冲突会造成网络客户不能正常工作,只 ...
- position:absolute实现垂直居中
一些图标通常要垂直居中 如下所示: 而css中没有直接的样式.需要我们自己调试. 我用了position:absolute;来实现. 要想使得position:absolute;有效,它的父元素必须也 ...
- Windows Azure 的开源 DNA
去年年底,第二期微软云加速器在中国正式启动,17 家创业公司入选.依靠云计算的 HTML 5 专业富媒体动画平台 Mugeda(乐享云)是其中之一.微软云加速器为创业公司提供大量支持,但 Mugeda ...
- 走出MFC子类化的迷宫
走出MFC子类化的迷宫 KEY WORDS:子类化 SUBCLASSWINDOW MFC消息机制 许多Windows程序员都是跳过SDK直接进行RAD开发工具[或VC,我想VC应不属于RAD]的学习 ...
- 虚拟机使用PuTTY、SSH Secure Shell Client前的配置
1 仅主机模式 2 检查子网IP 为192.168.154.0 3 进入虚拟机,检查IP地址 ip addr show 添加IP地址 ip addr add dev eno16777736 192.1 ...
- Baby Ming and Matrix games(dfs计算表达式)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...