总结大体实现思路:
1:创建一个ViewController,创建NavigationController将ViewController作为根视图
2:在ViewController中创建TableView 然后创建pilth的数据容器存储数据
3:创建数据源然后开始拆解数据,存储给meole里面。
4:根据需求分析和拆解出的数据,实现相应的,删,增,移动等
5:创建新的表视图
6:事件pus到下一个页面,利用属性传值的方式将其值传递到下一页面。
plist的一些小总结:
1:使用plist作为容器存储数据 使用KVC强制取值赋值到数据类的时候注意对应名称要一直,否则会崩溃
2:使用K值的时候,直接将数组存储的K值根据当前的分区下标拿出来用。不要去重复取字典中的,因为字典中的是无序的,每次取值不一定就是和你的K值数组相同,这样混合使用容易造成越界等等的各种内存问题,链接问题的出现,导致程序崩溃
3:分析plist的数据要一层一层的分析,分析需求最后你需要什么值,就要取到数据层当中的那一层的数据
编辑的总结:
编辑使用结合UINavigationController 使用,设置右侧的编辑按钮,调用系统的方法添加无需创建。
1:首先开启编辑的状态,根据UINavigationController的编辑按钮,使用系统的方法来调用实现
2:使用代理方法来重复确定一下开启了编辑状态 设立也可以设置特定的选项不打开编辑状态
3:使用代理方法,设置编辑状态的样式,这里的样式有三种,删除,添加,删除或添加。这里也可以设置特定分区设置单独样式
4:调用代理方法,实现处理删除添加的数据操作和UI操作 (先删除对应得数据,然后处理UI视图的操作)
平移的总结:
1:首先要打开编辑状态,当然这里前面的删除添加等操作的时候已经打开,无需重复操作
2:代理方法。确定打开平移的状态。
3:代理方法:处理平移事件的数据处理 这里有三个,1)先取出对应的移动前坐标值数据,2)然后删除移动前的左边数据,3)将数据添加到当前坐标位置。
实现代码,部分内容没办法上传仅上传主要代码
实现代码,部分内容没办法上传仅上传主要代码
--------------------------------------------------- 以下为代码部分
#import "RootViewController.h"
#import "Thecitylist.h"
#import "FirstTableViewController.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
//将tableView设置为一个属性
@property (strong,nonatomic) UITableView *tableView;
///定义一个可变数组的属性 接收对应的K值的
@property (strong,nonatomic) NSMutableArray *keyArray;
///定义一个可变字典属性,接收plist的数据
@property (strong,nonatomic) NSMutableDictionary *allDictionary;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建一个表视图
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
//设置行高
tableView.rowHeight = 50;
//设置代理方法
tableView.dataSource = self;
tableView.delegate = self;
//添加到视图上面
[self.view addSubview:tableView];
//属性值等于新的表视图
self.tableView = tableView;
//调用数据方法
[self MyName];
#pragma mark - 结合使用NavigationController
//UINavigationController 的设置 创建右边的编辑按钮,调用系统封装的方法
// self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//创建编辑按钮之后,需要创建一个响应事件
}
//如果想要对其进行增删改等操作需要打开编辑状态 使用UINavigationController控制是要让其更加的灵活一些
//打开编辑状态 右面编辑状态的响应事件
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
//调用父类的方法 这个地方的Bool的值不要写死,可以根据需要灵活写
[super setEditing:editing animated:animated];
//tableView打开编辑状态
[_tableView setEditing:editing animated:animated];
}
#pragma mark - 增删的编辑处理效果代理
///调用代理方法,确定打开了编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//调用代理方法,返回编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//编辑样式有三种:返回值为删除,添加,删除或添加
//根据返回值不同,返回不同的样式
//根据indexPath的值设置指定的区设置为添加样式
if (indexPath.section == 1) {
return UITableViewCellEditingStyleInsert;
}
//其他的返回为删除样式
return UITableViewCellEditingStyleDelete;
}
//提交处理增删的编辑处理效果 代理方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//判断当前的样式处于什么状态
if (editingStyle == UITableViewCellEditingStyleDelete) {
//如果处于删除的状态
//根据分区得出K值
NSString *str = self.keyArray[indexPath.section];
//根据K值从字典中取出对应的Value的数组值
NSMutableArray *array = [self.allDictionary objectForKey:str];
//根据目前所处的单元格下标处理数据,删除数组当中的数据
[array removeObjectAtIndex:indexPath.row];
//处理UI显示的效果,删除当前的单元格,重新载入视图显示在大家面前 这里的两个参数分别是当前的位置和消失的动画效果
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
//判断语句,当数组没得数据没删除为0的时候执行
if (array.count == 0) {
//删除Key数组当中的对应元素
[self.keyArray removeObject:str];
//删除字典中对应Key的Vable
[self.allDictionary removeObjectForKey:str];
//处理UI上面的视图显示
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:indexSet withRowAnimation:(UITableViewRowAnimationLeft)];
}
}else if (editingStyle ==UITableViewCellEditingStyleInsert){
//如果处于添加的样式
//初始化一个数据的model的对象
Thecitylist *theci = [[Thecitylist alloc] init];
//赋值
theci.Name = @"地球";
theci.population = @"亚洲";
//根据当前所处分区,取出Key
NSString *key = self.keyArray[indexPath.section];
//根据Key取出对应的value的数组分区的对象数组
NSMutableArray *muarray = [self.allDictionary objectForKey:key];
//根据当前的所处位置的坐标 将新的数据插入到数组当中去
[muarray insertObject:theci atIndex:indexPath.row];
//处理UI视图上面的显示数据
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
}
}
#pragma mark - 移动的编辑处理效果代理
//代理方法,打开平移的设置开关
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//代理方法 平移的数据编辑处理方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//根据当前的分区取得当前的数据的K值
NSString *key = self.keyArray[sourceIndexPath.section];
//根据K值取出当前分区所在的数组数据
NSMutableArray *muArray = [self.allDictionary objectForKey:key];
//创建一个model的数据类对象 便于存储数据 临时的容器
Thecitylist *thcity = [[Thecitylist alloc] init];
//取出移动前的坐标位置的数据
thcity = muArray[sourceIndexPath.row];
//删除当前坐标数据在数组中的存在
[muArray removeObjectAtIndex:sourceIndexPath.row];
//将取出的数据,根据当前坐标插入到数组当中
[muArray insertObject:thcity atIndex:destinationIndexPath.row];
}
//禁止跨区移动的代理方法
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//判断,当前鼠标拖动到的位置是否和移动前的位置,处于同一个分区
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
//如果处于同一个分区的话,鼠标结束,允许移动到当前位置,返回当前位置
return proposedDestinationIndexPath;
}
//如果不是处于同一个分区里面,禁止移动返回到移动前的位置
return sourceIndexPath;
}
#pragma mark - 推出下一个页面的处理
//代理方法 点击事件,推出下一个页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//创建推出的页面对象 这里的对象为表视图的TableViewController对象
FirstTableViewController *fireVC = [[FirstTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
//根据当前的位置得出K值
NSString *key = self.keyArray[indexPath.section];
//根据K值取出当前分区的分区的数组值
NSMutableArray *array = [self.allDictionary objectForKey:key];
//根据当前的坐标位置取出点击当前的位置的坐标的单元格model的对象 根据属性传值给下一个页面
fireVC.theci = array[indexPath.row];
//推出下一个页面
[self.navigationController pushViewController:fireVC animated:YES];
}
#pragma mark - 必须实现的两个代理方法 cell的样式和每个分区的个数
//创建每一分区有多少行 代理方法 必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//根据对应分区坐标取出对应的K值
NSString *key = self.keyArray[section];
//根据K值取出对应的分区数组
NSMutableArray *array = [self.allDictionary objectForKey:key];
//返回当前数组元素的个数,从而创建相应数量的行数
return array.count;
}
//创建每一行的样式 代理方法,必须实现的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//使用重用机制创建cell
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"CellView"];
if (Cell == nil) {
Cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"CellView"];
}
//根据当前的所在分区得出当前K值
NSString *str = self.keyArray[indexPath.section];
//创建一个model的数据类的对象
Thecitylist *teci = [[Thecitylist alloc] init];
///创建数组,根据当前的K值取出当前分区内所有的元素数组
NSMutableArray *array = [self.allDictionary objectForKey:str];
//根据当前的坐标得出当前的数组对象
teci = array[indexPath.row];
//赋值给cell显示
Cell.textLabel.text = teci.Name;
Cell.detailTextLabel.text = teci.population;
return Cell;
}
#pragma mark - 设置分区的个数和标题
//设置分区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//根据K值得个数设置分区个数
return self.keyArray.count;
}
//设置分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//根据得到的K值得数组赋值标题的内容
return self.keyArray[section];
}
#pragma mark - 数据的处理
//处理数据
- (void)MyName{
//获取字符串的链接
NSString *string = [[NSBundle mainBundle] pathForResource:@"Thecitylist" ofType:@"plist"];
//获取字典数据
NSDictionary *diction = [NSDictionary dictionaryWithContentsOfFile:string];
//初始化属性值数组和字典
self.keyArray = [NSMutableArray array];
self.allDictionary = [NSMutableDictionary dictionary];
//拆解数据
for (NSString *key in diction) {
//此时取出的是城市的相关信息
NSArray *array = diction[key];
//创建一个临时数组接收当前的对象
NSMutableArray *citysArr = [NSMutableArray array];
//遍历数组,取值出来对应的数据
for (NSDictionary *dic in array) {
//创建model类的对象
Thecitylist *theCity = [[Thecitylist alloc] init];
//使用KVC的方式强制取值赋值给数据类对象
[theCity setValuesForKeysWithDictionary:dic];
//将数据类对象添加到临时数组
[citysArr addObject:theCity];
}
//根据K值,将存储有多个数据类对象的数组添加到属性字典当中
[self.allDictionary setObject:citysArr forKey:key];
//将K值添加到熟悉数组当中
[self.keyArray addObject:key];
}
}