#import "RootViewController.h"
#import "Student.h"
#import "DetailViewController.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) NSMutableArray *group;//分组名数组
@property(strong,nonatomic) NSMutableDictionary *dataDict;//所有对象的容器
@property(strong,nonatomic) UITableView *tableView;
@end
static NSString * const systemCellReuseIdentifier = @"systemCellReuseIdentifier";
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Student *student = [[Student alloc] init];
// NSDictionary *dict = [[NSDictionary alloc] init];
// [student setValuesForKeysWithDictionary:dict];
//获取当前应用程序包文件
NSBundle *bundle = [NSBundle mainBundle];
//获取报属性列表(plist)文件路径
NSString *path = [bundle pathForResource:@"CLASS151042" ofType:@"plist"];
//根据plist文件创建对应的字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
// NSLog(@"%@",dict);
//获取所有分组
self.group = [[dict allKeys ]mutableCopy];
//排序
[self.group sortUsingSelector:@selector(compare:)];
self.dataDict = [[NSMutableDictionary alloc] initWithCapacity:26];
for (NSString *key in dict) {
NSMutableArray *tempGroup = [[NSMutableArray alloc] initWithCapacity:30];
for (NSDictionary *studentDic in dict[key]) {
Student *stu = [[Student alloc ] initWithDictionary:studentDic];
[tempGroup addObject:stu];
}
[self.dataDict setObject:tempGroup forKey:key];
}
NSLog(@"%@",[self.dataDict[@"M"] lastObject]);
self.view.backgroundColor = [UIColor grayColor];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
//注册Cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:systemCellReuseIdentifier];
[self.view addSubview:self.tableView];
}
#pragma mark = === tableView代理===
//返回分区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.group.count;
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//根据当前的分区找到对应的分组名
NSString *key = [self.group objectAtIndex:section];
//根据分组名找到对应的数组
NSArray *array = [self.dataDict objectForKey:key];
//返回数组元素个数作为分组行数
return array.count;
// return [self.dataDict[self.group[section]]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:systemCellReuseIdentifier];
//找到对应的分区名字
NSString *key = self.group[indexPath.section];
//找到对应的数组
NSArray *group = self.dataDict[key];
//找到数组中的某一个对象
Student *student = group[indexPath.row];
//将数据显示在Cell上
cell.textLabel.text = student.name;
return cell;
}
//设置分区
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.group[section];
}
//设置索引
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.group;
}
//页面跳转传值
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = self.group[indexPath.section];
NSArray *array = self.dataDict[key];
DetailViewController *detailVC= [[DetailViewController alloc] init];
detailVC.tempString = array[indexPath.row];
[self.navigationController pushViewController:detailVC animated:NO];
}
//让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[ self.tableView setEditing:YES animated:YES];
}
//指定tableView那些行可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return NO;
}
return YES;
}
//指定tableView编辑的样式(添加,删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
//编辑完成(先操作数据源,在修改UI)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
//UITableView移动
//让tableView处于编辑状态
//-(void)setEditing:(BOOL)editing animated:(BOOL)animated
//指定tableView那些行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//移动完成
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSLog(@"从第%ld行移动到第%ld行", sourceIndexPath.row + 1, destinationIndexPath.row + 1);
//(1)保留原来的数据
NSString *tempString = [self.group objectAtIndex:sourceIndexPath.row];
//(2)删除旧位置上的数据
[self.group removeObjectAtIndex:sourceIndexPath.row];
//(3)在新的位置上插入数据
[self.group insertObject:tempString atIndex:destinationIndexPath.row];
for (NSString *tempString in self.group) {
NSLog(@"%@", tempString);
}
}
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
Student *student = self.tempString;
UIImage *image = [UIImage imageNamed:student.picture];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 170, 250)];
imageView.backgroundColor = [UIColor whiteColor];
imageView.image = image;
[self.view addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 80, 150, 30)];
label.text = [NSString stringWithFormat:@"姓名 :%@",student.name];
[self.view addSubview:label];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 120, 150, 30)];
label1.text = [NSString stringWithFormat:@"性别 :%@",student.gender];
[self.view addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 160, 150, 30)];
label2.text = [NSString stringWithFormat:@"年龄 :%@",student.age];
[self.view addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 200, 30)];
label3.text = [NSString stringWithFormat:@"电话号码 :%@",student.phoneNumber];
[self.view addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(200, 240, 150, 30)];
label4.text = [NSString stringWithFormat:@"QQ :%@",student.QQNumber];
[self.view addSubview:label4];
UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(30, 280, 360, 400)];
label5.text = [NSString stringWithFormat:@"爱好 :%@",student.hobby];
label5.numberOfLines = 0;
[self.view addSubview:label5];
}
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(strong,nonatomic)NSString *name;
@property(strong,nonatomic)NSString *gender;
@property(strong,nonatomic)NSString *age;
@property(strong,nonatomic)NSString *hobby;
@property(strong,nonatomic)NSString *phoneNumber;
@property(strong,nonatomic)NSString *QQNumber;
@property(strong,nonatomic)NSString *picture;
//自定义初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict;
@end
#import "Student.h"
@implementation Student
//重写无法正确赋值的key-Value对,保证KVC对model对象赋值不出错
-(void)setValue:(id)value forUndefinedKey:(nonnull NSString *)key {
NSLog(@"==%@",key);
}
//打印方法,辅助校验model对象的属性,查看是否成功
-(NSString *)description {
return [NSString stringWithFormat:@"%@,%@,%@,%@,%@",_name,_age,_gender,_hobby,_picture];
}
//自定义初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict{
if (self = [super init]) {
//setValuesForKeys是修改值
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end
#import "Student.h"
#import "DetailViewController.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic) NSMutableArray *group;//分组名数组
@property(strong,nonatomic) NSMutableDictionary *dataDict;//所有对象的容器
@property(strong,nonatomic) UITableView *tableView;
@end
static NSString * const systemCellReuseIdentifier = @"systemCellReuseIdentifier";
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Student *student = [[Student alloc] init];
// NSDictionary *dict = [[NSDictionary alloc] init];
// [student setValuesForKeysWithDictionary:dict];
//获取当前应用程序包文件
NSBundle *bundle = [NSBundle mainBundle];
//获取报属性列表(plist)文件路径
NSString *path = [bundle pathForResource:@"CLASS151042" ofType:@"plist"];
//根据plist文件创建对应的字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
// NSLog(@"%@",dict);
//获取所有分组
self.group = [[dict allKeys ]mutableCopy];
//排序
[self.group sortUsingSelector:@selector(compare:)];
self.dataDict = [[NSMutableDictionary alloc] initWithCapacity:26];
for (NSString *key in dict) {
NSMutableArray *tempGroup = [[NSMutableArray alloc] initWithCapacity:30];
for (NSDictionary *studentDic in dict[key]) {
Student *stu = [[Student alloc ] initWithDictionary:studentDic];
[tempGroup addObject:stu];
}
[self.dataDict setObject:tempGroup forKey:key];
}
NSLog(@"%@",[self.dataDict[@"M"] lastObject]);
self.view.backgroundColor = [UIColor grayColor];
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
//注册Cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:systemCellReuseIdentifier];
[self.view addSubview:self.tableView];
}
#pragma mark = === tableView代理===
//返回分区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.group.count;
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//根据当前的分区找到对应的分组名
NSString *key = [self.group objectAtIndex:section];
//根据分组名找到对应的数组
NSArray *array = [self.dataDict objectForKey:key];
//返回数组元素个数作为分组行数
return array.count;
// return [self.dataDict[self.group[section]]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:systemCellReuseIdentifier];
//找到对应的分区名字
NSString *key = self.group[indexPath.section];
//找到对应的数组
NSArray *group = self.dataDict[key];
//找到数组中的某一个对象
Student *student = group[indexPath.row];
//将数据显示在Cell上
cell.textLabel.text = student.name;
return cell;
}
//设置分区
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.group[section];
}
//设置索引
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.group;
}
//页面跳转传值
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = self.group[indexPath.section];
NSArray *array = self.dataDict[key];
DetailViewController *detailVC= [[DetailViewController alloc] init];
detailVC.tempString = array[indexPath.row];
[self.navigationController pushViewController:detailVC animated:NO];
}
//让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[ self.tableView setEditing:YES animated:YES];
}
//指定tableView那些行可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return NO;
}
return YES;
}
//指定tableView编辑的样式(添加,删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
//编辑完成(先操作数据源,在修改UI)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
//UITableView移动
//让tableView处于编辑状态
//-(void)setEditing:(BOOL)editing animated:(BOOL)animated
//指定tableView那些行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//移动完成
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSLog(@"从第%ld行移动到第%ld行", sourceIndexPath.row + 1, destinationIndexPath.row + 1);
//(1)保留原来的数据
NSString *tempString = [self.group objectAtIndex:sourceIndexPath.row];
//(2)删除旧位置上的数据
[self.group removeObjectAtIndex:sourceIndexPath.row];
//(3)在新的位置上插入数据
[self.group insertObject:tempString atIndex:destinationIndexPath.row];
for (NSString *tempString in self.group) {
NSLog(@"%@", tempString);
}
}
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
Student *student = self.tempString;
UIImage *image = [UIImage imageNamed:student.picture];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 170, 250)];
imageView.backgroundColor = [UIColor whiteColor];
imageView.image = image;
[self.view addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 80, 150, 30)];
label.text = [NSString stringWithFormat:@"姓名 :%@",student.name];
[self.view addSubview:label];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 120, 150, 30)];
label1.text = [NSString stringWithFormat:@"性别 :%@",student.gender];
[self.view addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 160, 150, 30)];
label2.text = [NSString stringWithFormat:@"年龄 :%@",student.age];
[self.view addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 200, 30)];
label3.text = [NSString stringWithFormat:@"电话号码 :%@",student.phoneNumber];
[self.view addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(200, 240, 150, 30)];
label4.text = [NSString stringWithFormat:@"QQ :%@",student.QQNumber];
[self.view addSubview:label4];
UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(30, 280, 360, 400)];
label5.text = [NSString stringWithFormat:@"爱好 :%@",student.hobby];
label5.numberOfLines = 0;
[self.view addSubview:label5];
}
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(strong,nonatomic)NSString *name;
@property(strong,nonatomic)NSString *gender;
@property(strong,nonatomic)NSString *age;
@property(strong,nonatomic)NSString *hobby;
@property(strong,nonatomic)NSString *phoneNumber;
@property(strong,nonatomic)NSString *QQNumber;
@property(strong,nonatomic)NSString *picture;
//自定义初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict;
@end
#import "Student.h"
@implementation Student
//重写无法正确赋值的key-Value对,保证KVC对model对象赋值不出错
-(void)setValue:(id)value forUndefinedKey:(nonnull NSString *)key {
NSLog(@"==%@",key);
}
//打印方法,辅助校验model对象的属性,查看是否成功
-(NSString *)description {
return [NSString stringWithFormat:@"%@,%@,%@,%@,%@",_name,_age,_gender,_hobby,_picture];
}
//自定义初始化方法
-(instancetype)initWithDictionary:(NSDictionary *)dict{
if (self = [super init]) {
//setValuesForKeys是修改值
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end