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 = [[[UINavigationControlleralloc]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:[UIScreenmainScreen].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 = [[NSBundlemainBundle]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 = [UIColororangeColor];
    //设置编辑按钮
    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 = [NSIndexSetindexSetWithIndex: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:[UIScreenmainScreen].bounds];
    self.view = detaileView;
    [detaileView release];

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

TableEdit UI相关推荐

  1. html表格行内编辑器,表格编辑制作软件——TableEdit

    原标题:表格编辑制作软件--TableEdit TableEdit for mac是专门设计了一个简单,干净,优雅的电子表格应用程序为OS X TableEdit设有简约而直观的界面,TableEdi ...

  2. 2022-2028年中国UI设计行业现状调研分析及发展趋势研究报告

    [报告类型]产业研究 [报告价格]4500起 [出版时间]即时更新(交付时间约3个工作日) [发布机构]智研瞻产业研究院 [报告格式]PDF版 本报告介绍了中国UI设计行业市场行业相关概述.中国UI设 ...

  3. 为什么UI线程中创建Handler可以不传Looper?

    一个APP程序的入口是ActivityThread的main方法,ActivityThread就是我们常说的主线程或UI线程,事实上它并不是一个线程,而是主线程操作的管理者. public stati ...

  4. Cocos Creator 预制的使用模板(一般用于UI)

    Cocos Creator里绑定properties @property(cc.Prefab)XXUIPrefab: cc.Prefab = null; 在预制上挂脚本 import { Consta ...

  5. HarmonyOS UI开发 AdaptiveBoxLayout(自适应盒子布局) 的使用

    AdaptiveBoxLayout 是什么 AdaptiveBoxLayout 是自适应盒子布局,该布局提供了在不同屏幕尺寸设备上的自适应布局能力,主要用于相同级别的多个组件需要在不同屏幕尺寸设备上自 ...

  6. HarmonyOS UI开发 TableLayout(表格布局) 的使用

    TableLayout 是什么 TableLayout  是表格布局,就是使用表格的方式划分子组件, 个人感觉应该也不是常用的布局,常用的还是选择DirectionalLayout和Dependent ...

  7. HarmonyOS UI开发 PositionLayout(位置布局) 的使用

    PositionLayout 是什么 PositionLayout 是位置布局,在PositionLayout中,子组件通过指定准确的x/y坐标值在屏幕上显示.(0, 0)为左上角:当向下或向右移动时 ...

  8. HarmonyOS UI开发 StackLayout(堆栈布局) 的使用

    StackLayout是什么 StackLayout 是堆栈布局,直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左上角,第一个添加到布 ...

  9. HarmonyOS UI开发 DependentLayout(依赖布局) 的使用

    DependentLayout是什么 DependentLayout是依赖布局,每个组件可以指定相对于其他同级元素的位置,或者指定相对于父组件的位置.(类似Android的相对布局) Dependen ...

最新文章

  1. linux 测试内存性能,Linux性能测试指标评估
  2. python详细安装步骤-Python的详细安装步骤
  3. JQuery-FullCalendar 多数据源实现日程展示
  4. Bootstrap 字体图标(Glyphicons)
  5. 机器学习笔记(十三)半监督学习
  6. wxWidgets:wxDir类用法
  7. 全面升级 | 阿里云中间件推出3款新品和3项产品新特性,加速企业中台落地
  8. centos7系统引导自动重启_分享一个实用的脚本——全自动一键批量性安装Linux系统...
  9. LinuxShell笔记3 条件控制和流程控制
  10. AcWing 827. 双链表
  11. 请说出三种减少页面加载时间的方法
  12. 简单的抓包_学习笔记
  13. idea运行java项目js中文乱码如何解决
  14. java注册功能实现
  15. VHDL:基于 FPGA 实时处理的双目测距系统
  16. Adaptable and Adaptive Hypermedia Systems
  17. 希望我能帮助你:关于软件开发的建议
  18. 阴谋还是悲剧?- 基于机器学习假设检验视角,看泰坦尼克号事件
  19. 爱创课堂每日一题第二十九天- ES6的了解?
  20. Linux 通配符和特殊符号

热门文章

  1. php如何导出xls表,php如何导出excel表格?
  2. 杰理之扫描设备【篇】
  3. 软件测试文档需求概述,1.软件测试概述
  4. seata启动闪退/报错
  5. 网人5.02和DZ7整合文件及简要说明
  6. appImage linux 上可执行文件 简介
  7. 解决com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link
  8. mysql查询前5000条记录分页显示_数据库 完整的查询语句,查询顺序及分页显示功能...
  9. 【IPFS应用开发】--IPNS加速器
  10. android 自定义多tab悬浮控件,Android编程实现自定义Tab选项卡功能示例