TableEditnbsp;UI_10
1.让tableView处于编辑状态,(默认所有的cell都处于编辑状态,默认下的编辑样式是删除)
2.设置哪些cell可以编辑
3.设置编辑的样式(删除,插入)
1.让tableView处于编辑状态
2.设置哪些cell可以移动
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
#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];
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
//设置数据源代理
tableView.dataSource = self;
//设置业务代理
tableView.delegate = self;
//将tableView 指定为rootViewController 根视图
self.view = tableView;
[tableView release];
}
[super viewDidLoad];
// NSLog(@"%@",diict);//验证是否取出来
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时不可以编辑
——————————————————————————————————
#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) {
[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"];
}
————————————————————————————————
#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];
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];
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
TableEditnbsp;UI_10相关推荐
- TableEditamp;nbsp;UI_10
1.tableView的编辑的步骤: 1.让tableView处于编辑状态,(默认所有的cell都处于编辑状态,默认下的编辑样式是删除) 2.设置哪些cell可以编辑 3.设置编辑的样式(删除, ...
最新文章
- Android之EditText的各种使用
- 如何用WORD制作三线表
- 【Python 小知识】[:-1] 和 [::-1]
- Android keystore/Keymaster的代码导读
- LeWeb – 2011 –综述
- python1234出栈_Python数据结构与算法3——栈和队列
- CVPR2021论文整理
- 基于springboot的社区核酸检测统计管理系统
- java答题库_JAVA题库
- 线性回归(一):一元线性回归(附python实现)
- volte短信流程-注册
- 区块链的供应链金融系统
- Java的第一个你好世界
- 英雄联盟——心得体会
- Linux文件管理 | Liunx 常用命令
- pc安装openwrt
- ffmpeg基础三:H264,从MP4文件获取(av_bsf_get_by_name(“h264_mp4toannexb“))和从TS流获取保存H264
- 容器化技术与微服务结合---docker(一)
- 1086oj java
- 用python画画的原理_matplotlib绘图的核心原理讲解
热门文章
- nba2k19数据查询方式
- Understanding and Using Assemblies and Namespaces in .NET
- 对于mysql数据库视图心得_MySQl心得4--5--数据库视图
- 员工奖金需要交税吗_企业为员工发福利 奖品奖金也需要缴税
- C#常用日期格式处理转换
- f1c100s入坑笔记(4)-uboot学习stepbystep-1
- 系统之家一键重装联想Win10图解
- sql行并列,列分行
- 王者荣耀手机助手服务器异常,如何解决王者荣耀助手登录网络异常
- 几行python代码制作桌面截图程序