准备1:添加了一个数据源Class25ContactList.plist
================================
准备2:创建一个CellModel类
字典中有几个键值对 就要创建几个属性
注意:属性的名字 就是键值对中的key 要完全一致
@interface CellModel : NSObject
@property (nonatomic,retain)NSString *name;
@property (nonatomic,retain)NSString *gender;
@property (nonatomic,retain)NSString *age;
@property (nonatomic,retain)NSString *phoneNumber;
@property (nonatomic,retain)NSString *hobby;
@property (nonatomic,retain)NSString *picture;
@end
在CellModel.m”中
kvc间接访问属性 保护方法 必须要实现的
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
可以捕获错误的key 可以打印出来
NSLog(@”%@”,key);
}
================================
准备3:创建一个视图控制器(接收传值的)
创建一个继承于UIViewController的控制器
@interface SecondViewController : UIViewController
声明属性用于接收数据
@property (nonatomic,retain)NSString *name;
@property (nonatomic,retain)NSString *gender;
@property (nonatomic,retain)NSString *age;
@property (nonatomic,retain)NSString *phoneNumber;
@property (nonatomic,retain)NSString *hobby;
@property (nonatomic,retain)NSString *picture;
@end
===================================
创建表视图
-(void)addTableView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
[tableView release];
}
添加数据处理的方法
- (void)setUpData
{
取出路径
NSString *path = [[NSBundle mainBundle]pathForResource:@”Class25ContactList” ofType:@”plist”];
用字典接收路径(路径里是一个字典)
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
创建一个字典保存新的字典
self.dataDic = [NSMutableDictionary dictionary];
取出所有key
NSArray *keys = dic.allKeys;
for (int i = 0; i < keys.count; i++) {
取出每一个key
NSString *key = keys[i];
创建一个临时数组
NSMutableArray *values = [NSMutableArray array];
取出每一个数组
NSArray *temparray = dic[key];
遍历每一个temparray
for (NSDictionary *oneDic in temparray) {
创建model
CellModel *model = [[CellModel alloc] init];
给model赋值
[model setValuesForKeysWithDictionary:oneDic];
你给我一个字典 我还你一个赋值完成model
[values addObject:model];
释放
[model release];
}
重新构建 字典的键值对
[self.dataDic setObject:values forKey:key];
}
取出所有的key
NSArray *akeys = self.dataDic.allKeys;
对key进行排序
self.sortKeysArray = [akeys sortedArrayUsingSelector:@selector(compare:)];
}
设置有多少个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataDic.count;
}
设置每一区有多少行(必须实现)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
用分区 取出对应的key
NSString *key = self.sortKeysArray[section];
用这个key取出对应的数组
NSArray *values = self.dataDic[key];
return values.count;
}
必须实现
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @”Mycell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}
取出对应的key
NSString *key = self.sortKeysArray[indexPath.section];
取出key对应的value
NSArray *values = self.dataDic[key];
用model接收数组中的model
CellModel *model = values[indexPath.row];
cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.phoneNumber;
return cell;
}
设置分区标头的标题
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
return self.sortKeysArray[section];
}
设置右边的按钮
- (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
{
return self.sortKeysArray;
}
实现点击跳转传值
- (void)tableView:(UITableView )tableView didUnhighlightRowAtIndexPath:(NSIndexPath )indexPath
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
取出对应分区的key
NSString *key = self.sortKeysArray[indexPath.section];
取出对应的values
NSArray *values = self.dataDic[key];
取出对应的model
CellModel *model = values[indexPath.row];
传值
secondVC.name = model.name;
secondVC.age = model.age;
secondVC.gender = model.gender;
secondVC.phoneNumber = model.phoneNumber;
secondVC.picture = model.picture;
secondVC.hobby = model.hobby;
调转页面
[self.navigationController pushViewController:secondVC animated:NO];
}
===================================