多组表格的思路
一、分析plist文件中的形式,创建model层数据 NSArray —> NSDictionary —>NSArray
最外层的数组
+ (instancetype)carGroupsWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
[self setValue:dict[@"title"] forKey:@"title"];
self.carGroups =
[SUNCar carsWithArray:dict[@"cars"]];
return self;
}
+ (NSArray *)carGroups
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array)
{
[arrayM addObject:[self carGroupsWithDict:dict]];
}
return arrayM;
}
最里层的数组
+ (instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
[self setValuesForKeysWithDictionary:dict];
return self;
}
+ (NSArray *)carsWithArray:(NSArray *)array
{
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array)
{
[arrayM addObject:[self carWithDict:dict]];
}
return arrayM;
}
二、懒加载UITableView
- (UITableView
*)tableView
{
if (_tableView
==
nil) {
_tableView = [[UITableView
alloc]
initWithFrame:self.view.bounds
style:UITableViewStylePlain];
_tableView.dataSource
=
self;
[self.view
addSubview:_tableView];
}
return
_tableView;
}
三、实现代理方法
#pragma mark -
数据源方法
//
分组总数
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView
{
return
self.carGroups.count;
}
//
每一组的总数
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section
{
HMCarGroup *group =
self.carGroups[section];
return group.cars.count;
}
//
单元格
- (UITableViewCell
*)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//
可重用标示符
static
NSString *ID =
@"Cell";
//
让表格缓冲区查找可重用cell
UITableViewCell
*cell = [tableView
dequeueReusableCellWithIdentifier:ID];
//
如果没有找到可重用cell
if (cell ==
nil) {
//
实例化cell
cell = [[UITableViewCell
alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ID];
}
//
设置cell内容
// 1>
取出数据模型
HMCarGroup *group =
self.carGroups[indexPath.section];
HMCar *car = group.cars[indexPath.row];
// 2>
设置数据
cell.imageView.image
= [UIImage
imageNamed:car.icon];
cell.textLabel.text
= car.name;
return cell;
}
//
标题
- (NSString
*)tableView:(UITableView
*)tableView titleForHeaderInSection:(NSInteger)section
{
//
找到group
HMCarGroup *group =
self.carGroups[section];
return group.title;
}
//
右侧索引列表
- (NSArray
*)sectionIndexTitlesForTableView:(UITableView
*)tableView
{
//
返回self.carGroup中title的数组
// NSMutableArray *arrayM = [NSMutableArray array];
// for (HMCarGroup *group in self.carGroups) {
// [arrayM addObject:group.title];
// }
// return arrayM;
// KVC是cocoa的大招
//
用来间接获取或者修改对象属性的方式
//
使用KVC在获取数值时,如果指定对象不包含keyPath的"键名",会自动进入对象的内部查找
//
如果取值的对象是一个数组,同样返回一个数组
// NSArray
*array = [self.carGroups
valueForKeyPath:@"cars.name"];
return [self.carGroups
valueForKeyPath:@"title"];
}
多组表格的思路
原文:http://blog.csdn.net/itcontend/article/details/41983289