#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的添加,删除,移动的更多相关文章

  1. WPF下的Richtextbox中实现表格合并,添加删除行列等功能

    .Net中已有现在的方法实现这些功能,不过可能是由于未完善,未把方法公开出来.只能用反射的方法去调用它. 详细信息可以查看.Net Framework 的源代码 http://referencesou ...

  2. 编辑 Ext 表格(一)——— 动态添加删除行列

    一.动态增删行 在 ext 表格中,动态添加行主要和表格绑定的 store 有关, 通过对 store 数据集进行添加或删除,就能实现表格行的动态添加删除.   (1) 动态添加表格的行  gridS ...

  3. Angular-表单动态添加删除

    angular本身不允许去操作DOM,在angular的角度来说,所有操作都以数据为核心,剩下的事情由angular来完成.所以说,想清楚问题的根源,解决起来也不是那么困难. 前提 那么,要做的这个添 ...

  4. 用Javascript动态添加删除HTML元素实例 (转载)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. [CentOS]添加删除用户

    摘要 在安装CentOS的时候,我们只设置了root,类似windows的超级管理员.当然我们在工作的时候,为了安全考虑,不可能对外开发root,一方面是从安全的角度,另一方面也是方便管理. 添加删除 ...

  6. iOS仿网易新闻栏目拖动重排添加删除效果

    仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...

  7. 百度地图API示例之添加/删除工具条、比例尺控件

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

  8. Android 动态添加删除ExpandableListView的item的例子

    这个例子可以学习到如下几点: 1.通过自定义Dialog(单独布局的xml文件进行弹出显示) 2.通过menu点击监听添加,删除view中的items 3.点击ExpandableListView中g ...

  9. jQuery添加删除元素

    $(document).ready(function () { $('#radioExtranet').on('click', function () { showProjectInformation ...

随机推荐

  1. 【转】关于C++程序的编码问题

    引用自:http://blog.chinaunix.net/uid-26790551-id-3190813.html 我们传统的程序基本都只在Windows或只在Linux下运行,Windows程序使 ...

  2. MySQL查询优化之explain

    在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...

  3. Mysql优化之创建高性能索引(二)

    1.索引的优点 索引可以让服务器快速地定位到表的指定位置.总结下来有三大优点: 索引大大减少了服务器需要扫描的数据量 索引可以帮助服务器避免排序和临时表 索引可以将随机I/O变为顺序I/O 2.高性能 ...

  4. mysql数据库数据恢复方案概括总结

    方案一:(传统方案) 备份+binlog日志增量: 方案二:(针对update.delete语句忘加where的情况) Binlog日志文件中保存有错误操作之前和之后的两组数据,将错误操作之前的数据修 ...

  5. python执行外部程序模块pyshell

    写python程序的时候需要用到调用外部命令的模块,看了一下,还真不少,头疼,用着不顺手.根据官网推荐,我根据官网的subprocess模块定制了一个自己的shell,同时借鉴了github上面的sh ...

  6. Oracle ORA-01555(快照过旧)

    一.引言: [oracle@yft yft]$ oerr ora 01555 01555, 00000, "snapshot too old: rollback segment number ...

  7. Node.js log2: ERR when execute command >npm install

    1.Node.js创建项目 项目microblog创建成功,提示:cd  microblog& npm install 项目创建完成时的目录如下图所示: 2.Node.js错误 如题所言: E ...

  8. linux之SQL语句简明教程---SELECT

    SQL是用来做什么的呢?一个最常用的方式是将资料从数据库中的表格内选出.从这一句回答中,我们马上可以看到两个关键字: 从 (FROM) 数据库中的表格内 选出 (SELECT).(表格是一个数据库内的 ...

  9. #include <set>

    1 multiset 多重集合(multiset) 允许存在两个次序相等的元素的集合 <set> 2 set 集合(set) 由节点组成的红黑树,每个节点都包含着一个元素,节点之间以某种作 ...

  10. hdu 5620 KK's Steel(推理)

    Problem Description Our lovely KK has a difficult mathematical problem:he has a N(1≤N≤1018) meters s ...