1、tableView的编辑的步骤:

 1.让tableView处于编辑状态,(默认所有的cell都处于编辑状态,默认下的编辑样式是删除)

2.设置哪些cell可以编辑

 3.设置编辑的样式(删除,插入)

 4.提交编辑结果(先修改数据源,再修改UI)
 tableView的移动的步骤:

1.让tableView处于编辑状态

 2.设置哪些cell可以移动

 3.提交移动结果
图片素材:
本节素材plist文件下载:http://pan.baidu.com/s/1c0yiV8O
————————————————————————————————
AppDelegate.m
 
self.window.rootViewController
=
[[[UINavigationController
alloc]initWithRootViewController:[RootViewController
alloc]]autorelease];
RootViewController.m
#import "RootViewController.h"

#import "DetailViewController.h"

@interface RootViewController
()<</span>UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableDictionary
*dict;
@property(nonatomic,retain)NSMutableArray *orderKeys;
@end

@implementation RootViewController

-(void)dealloc{

   
self.dict = nil;

   
self.orderKeys = nil;

   
[super
dealloc];

}
重写loadView方法,将UITableView指定为视图控制器的对象
-
(void)loadView{

   
UITableView
*tableView =
[[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];

//设置数据源代理

   
tableView.dataSource = self;

   
//设置业务代理

   
tableView.delegate = self;

   
//将tableView 指定为rootViewController
根视图

   
self.view = tableView;

   

   
[tableView release];

}


-
(void)viewDidLoad {

   
[super
viewDidLoad];
   
self.view.backgroundColor = [UIColor brownColor];
 
  配置导航条的方法
   
[self
configureNavigationContent];
 
  从plist文件读取数据
   
[self
readDataFromPlist];
}

从plist文件读取数据
-
(void)readDataFromPlist{
  
1.获取文件路径
 NSString
*filePath =
[[NSBundle mainBundle]pathForResource:@"Contacts.plist"
ofType:nil];
   
  
2.根据文件路径初始化字典对象,因为此时文件的最外层是字典
   
self.dict = [NSMutableDictionary
dictionaryWithContentsOfFile:filePath];

//   
NSLog(@"%@",diict);//验证是否取出来
   
 
  copy一个一摸一样的字典出来,枚举copy出来的字典,修改原来的字典
   
NSDictionary
*copyDict =
[NSDictionary dictionaryWithDictionary:self.dict];
 
  遍历字典
   
for (NSString *key in copyDict) {

       
NSArray
*array =
copyDict[key];

   
//初始化可变数组

       
NSMutableArray
*mArr =
[NSMutableArray arrayWithArray:array];

       
[self.dict setValue:mArr forKey:key];

    }

   

   

   
//3.获取字典中所有的key值

NSArray
*keys
=  self.dict.allKeys;

   
//4.对数组中keys排序

NSArray *array = 
[keys sortedArrayUsingSelector:@selector(compare:)];

//5.初始化存放key的数组

self.orderKeys = [NSMutableArray arrayWithArray:array];

   

}


//配置导航条的显示的内容

- (void)configureNavigationContent{

   
self.navigationItem.title  =
@"通讯录";

   
self.navigationController.navigationBar.barTintColor
= [UIColor orangeColor];

   
//设置编辑按钮

   
self.navigationItem.rightBarButtonItem
= self.editButtonItem;

}
//重写点击Edit按钮方法

- (void)setEditing:(BOOL)editing
animated:(BOOL)animated{

   
[super
setEditing:editing animated:animated];
//   
NSLog(@"%d",editing);   
验证可编辑状态
//   
editing   
为1时可以编辑,为0时不可以编辑

   
[(UITableView
*)self.view 
setEditing:editing animated:YES ]; 
}



——————————————————————————————————
#pragma mark
必须实现的数据源代理方法
//2.配置哪些cell可以编辑

- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//   
if (indexPath.section < 3) {
//       
return YES;
//   
}
//   
return NO;

   
return
indexPath.section <</span> 3 ? YES : NO;

}
//设置deligt为删除

- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath{

   
return
@"删除";

}
//4.提交编辑操作

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

   //先修改数据源再更新UI(界面)

//1.根据分区索引获取key值,确定要删除的cell在哪个分区(eg:B分区
D分区)

   
NSString
*key 
= self.orderKeys[indexPath.section];

   
//2.根据key值拿到字典中对应的分组

NSMutableArray
*group =
self.dict[key];

   

   

   
//删除

   
if (editingStyle ==
UITableViewCellEditingStyleDelete)
{

    
//处理删除操作
       
if (1 == group.count) {//删除整个分组
           
//1.先删除数据源,从字典中移除key值

[self.dict removeObjectForKey:key];

//删除索引栏数组中的对应的元素

[self.orderKeys removeObjectAtIndex:indexPath.section];

//2.更新UI界面

//创建一个NSIndexSex
对象,使用分区下标初始化

           
NSIndexSet
*indexSet =
[NSIndexSet indexSetWithIndex:indexPath.section];

[tableView deleteSections:indexSet withRowAnimation:(UITableViewRowAnimationLeft)];

}else{//删除对应的cell即可

//先删除数据源

           
[group removeObjectAtIndex:indexPath.row];

//再更新UI界面

           
//tableView删除是可以删除多行

[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:(UITableViewRowAnimationLeft)];

}

}else{//添加

       
//1.准备要插入的数据

NSDictionary
*dic =
@{@"name":@"黄凯",@"gender":@"妖",@"age":@"25",@"phone":@"3838438",@"imageName":@"1.png",@"says":@"千人斩"};

       
//2.修改数据源

       
[group insertObject:dic atIndex:indexPath.row];

      
//3.更行UI界面

[tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:(UITableViewRowAnimationRight)];

}

   

}



- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView{

   
//根据字典中键值对的个数返回二分区个数

return
self.dict.count;

}


//返回row个数

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

   
//1.获取对应分区下标所对应的key值

NSString
*key =
self.orderKeys[section];

   

  
//2.根据key值取出字典的value值并且返回数组元素的个数

return
[self.dict[key]count];

}
//返回区头标题

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section{

   
//返回对应分区区头

return
self.orderKeys[section];

}
//返回右侧索引栏

- (NSArray *)sectionIndexTitlesForTableView:(UITableView
*)tableView{

   
return
self.orderKeys;

   

}



- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   

   
//1.重建重用id标示

static
NSString
*identifier =
@"cell";

   
//2.tableView对象去重用池取可以重用的cell

UITableViewCell *cell
=   
[tableView dequeueReusableCellWithIdentifier:identifier];

//3.判断有没有取到cell

if (cell == nil) {

       
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle)
reuseIdentifier:identifier]autorelease];

}



   
//展示数据步骤:

   
//1.根据cell所在分区的索引获取对应的key值

NSString
*key =
self.orderKeys[indexPath.section];

   
//2.根据key值获取字典中的value值

NSArray
*values =
self.dict[key];

   
//3.根据row的索引获取数组中对应的元素

NSDictionary
*pDict =
values[indexPath.row];
//   
NSLog(@"%@",pDict);//验证是否取得字典中的人信息

   
//4.取出字典中数据用cell展示

cell.imageView.image = [UIImage imageNamed:pDict[@"imageName"]];

   
cell.textLabel.text = pDict[@"name"];

   
cell.detailTextLabel.text = pDict[@"phone"];
 
 
   
return
cell;

}

————————————————————————————————
#pragma mark 
tableView 的移动
//设置哪些行可以移动

- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

   
return
YES;

}
//提交移动后的操作

- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath{
//   
sourceIndexPath   cell原来的位置
//   
destinationIndexPath 
cell移动之后的位置
//  
移动操作不需要更新UI界面,因为移动的时候UI界面已经发生了变化,此时只需修改数据源即可

//首先获取cell展示的数据所在的数组

//1.取出key值

NSString
*key =
self.orderKeys[sourceIndexPath.section];

//2.取出字典中key值对应的数组

NSMutableArray
*mArr
=  self.dict[key];

   

   
//3.将原来的数据取出来一份保存起来

NSDictionary
*dic =
[mArr[sourceIndexPath.row]retain];//2

   
//4.删除数组中原来位置的元素

[mArr
removeObjectAtIndex:sourceIndexPath.row];//1

//5.将元素插入到数组中新的位置

[mArr
insertObject:dic atIndex:destinationIndexPath.row];//2

//释放dic

    [dic
release];//1

   

}


//如果移动过后不是在原来的分区,则取消移动结果返回原来位置

- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{

//   
sourceIndexPath   cell原来的位置
//   
proposedDestinationIndexPath 
cell移动之后的位置

   
//如果在同一分区则让cell移动,返回移动后的位置

if (sourceIndexPath.section
==
proposedDestinationIndexPath.section) {

return
proposedDestinationIndexPath;

   
}else{//如果不在同一分区,返回移动之前的位置

return
sourceIndexPath;

}

   

}

——————————————————————-——————————
#pragma mark
业务代理方法的实现
//3.设置tableView的编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView
*)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

   

//   
if (indexPath.section < 2) {
//       
return UITableViewCellEditingStyleDelete;
//   
}else{
//       
return UITableViewCellEditingStyleInsert;
//   
}
//   
return NO;

   
return
indexPath.section <</span> 2 ? UITableViewCellEditingStyleDelete
: UITableViewCellEditingStyleInsert;

}
//设置行高

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   
return
80.0;

}



- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   
DetailViewController
*detailVC =
[[DetailViewController
alloc]init];

NSString
*key =
self.orderKeys[indexPath.section];

   
NSMutableArray
*mArr =
self.dict[key];

   
//取出数组中的字典并赋值给属性

NSDictionary
*dic =
mArr[indexPath.row];

   
detailVC.dic = dic;

   

   
[self.navigationController
pushViewController:detailVC
animated:YES];

   
[detailVC release];

   

}
=======================================================
DetailViewController.h
@property(nonatomic,retain)NSDictionary *dic;
DetailViewController.m
-
(void)dealloc{

   
self.dic = nil;

   
[super
dealloc];

}

- (void)viewDidLoad {

   
[super
viewDidLoad];

       
self.view.backgroundColor = [UIColor whiteColor];

   
[(DetailView *)self.view assignAllController:self.dic];

}



- (void)loadView{

   
DetailView
*detaileView =
[[DetailView alloc]initWithFrame:[UIScreen mainScreen].bounds];

   
self.view = detaileView;

   
[detaileView release];

}
最终效果:



===================================================
传值不做介绍,仅供参考!
欢迎学习本文,未经许可,禁止转载!

TableEdit&nbsp;UI_10的更多相关文章

  1. UITableView与UISearchController搜索及上拉加载,下拉刷新

    #import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...

  2. UITableView相关知识点

    //*****UITableView相关知识点*****// 1 #import "ViewController.h" // step1 要实现UITableViewDataSou ...

  3. Xcodeproject详解

    前言 在 iOS 开发过程中,我们经常会在 Xcode 里面做一些配置,比如添加系统库.第三方库,修改证书配置文件,修改编译属性等等. 在这个过程里面,一般大家仅仅只是根据经验来配置这些,并没有比较清 ...

  4. Ext.net

    .FileTypeName=="附件")                 {                      command.text="上传";   ...

  5. bjui给出的一个标准应用的首页

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  6. JavaWeb项目开发案例精粹-第6章报价管理系统-07View层

    1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...

  7. BJUI 转

    B-JUI 前端框架B-JUI(Bootstrap for DWZ)是一个富客户端框架,基于DWZ-jUI富客户端框架修改. 本文是B-JUI中文使用手册,包括使用示例代码,感兴趣的同学参考下. 概览 ...

  8. [ipsec][strongswan] 使用wireshark查看strongswan ipsec esp ikev1 ikev2的加密内容

    一,编译,启用strongswan的save-keys plugin ./configure --prefix=/root/OUTPUT --exec-prefix=/root/OUTPUT --en ...

  9. jQuery EasyUI window窗口使用实例

    需求:点击[增加]按钮,弹出窗口,并对所有输入项内容进行校验,校验通过就提交给后台的action处理,没有通过校验就弹窗提示.  <!DOCTYPE html> <html> ...

随机推荐

  1. ionic笔记

    ionic教程网站:http://www.ionic.wang/ 1.ui-router路由简介 https://blog.csdn.net/mcpang/article/details/551015 ...

  2. 一口一口吃掉Hibernate(七)——继承映射

    前几篇博文中讲到了常用的几种关联映射.其实hibernate中还有一种"省劲儿"的映射,那就是--"继承映射". 学了这么多的关系映射了,继承映射,从字面上也能 ...

  3. MySQL查看数据库信息

    使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小.字符集.用户等等.下面总结了一些查看数据库相关信息的命令 1:查看显示所有数据库 mysql> show databases; ...

  4. 解决警告: Setting property 'source' to 'org.eclipse.jst.jee.server_:' did not find a matching property.的方法

    今天第一次搭建struts2框架,跟着网上的教程导入对应的jar包之后就开始写登录的jsp页面,但是运行时出现了问题, 浏览器显示"The requested resource is not ...

  5. MySQL 连接的使用

    MySQL 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JOIN ...

  6. 安卓高级 特效动画ExplosionField和 SmoothTransition

    本教程所有图片为github上的所无法正常访问请科学上网 SmoothTransition 展示效果 github:源码地址 使用方法 你能通过一行代码使用上面所有的动画 @Override prot ...

  7. 大规模WebGL应用引发浏览器崩溃的几种情况及解决办法

    一般的Web应用基本上不会导致浏览器崩溃,写Javascript代码也不需要管理内存资源,基本也不需要考虑内存"泄露"的问题.随着H5的崛起,越来越多的原本在桌面端的软件也改头换面 ...

  8. 一起聊聊什么是P问题、NP问题、NPC问题

    概念 P问题:如果一个问题可以找到一个能在多项式的时间里解决它的算法,那么这个问题就属于P问题.通常NOI和NOIP不属于P类问题,我们常见到的一些信息奥赛的题目都是P问题. NP问题:可以在多项式的 ...

  9. 【安卓开发】为什么不能往Android的Application对象里存储数据

    在一个App里面总有一些数据需要在多个地方用到.这些数据可能是一个 session token,一次费时计算的结果等.通常为了避免activity之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...

  10. iOS界面不能点击(tableView 的cell 不能使用点击事件,tableView也不能上下滚动)

    iOS开发中间遇到了很多问题,有些后来又遇到,竟然忘记怎么处理了,所以还是来写下博客记录自己遇到的问题,方便自己也方便别人. 之前想的是项目小,就用storyboard在绘制界面,但是项目慢慢的扩展, ...