把數(shù)據(jù)全部存在plist文件中,最外面的數(shù)組包含2個(gè)字典,字典里面分別保存title與car钾埂,car是一個(gè)數(shù)據(jù),里面有2個(gè)字典科平,字典里面分別保存了車的名字與圖片的名字褥紫。接下來,我們要構(gòu)建數(shù)據(jù)模型瞪慧,把每一行當(dāng)作一個(gè)模型髓考,里面有車的名字與圖片:
```
#import@interface CarModule : NSObject
//名字
@property (nonatomic,copy) NSString *name;
//圖片
@property (nonatomic,copy) NSString *icon;
+(instancetype)carWithDict:(NSDictionary*)dic;
-(instancetype)initWithDict:(NSDictionary*)dic;
@end
#import "CarModule.h"
@implementation CarModule
+(instancetype)carWithDict:(NSDictionary *)dic{
return [[self alloc]initWithDict:dic];
}
```
-(instancetype)initWithDict:(NSDictionary *)dic{
if (self=[super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
@end
接下來創(chuàng)建每一個(gè)區(qū)域的模型,
#import@interface CarGroup : NSObject
//標(biāo)題
@property (nonatomic,copy)NSString *title;
//車的數(shù)組
@property (nonatomic,copy)NSArray *carGroupArray;
+(instancetype)carGroupWithDict:(NSDictionary*)dic;
-(instancetype)initWithDict:(NSDictionary*)dic;
@end
#import "CarGroup.h"
#import "CarModule.h"
@implementation CarGroup
+(instancetype)carGroupWithDict:(NSDictionary *)dic{
return [[self alloc]initWithDict:dic];
}
-(instancetype)initWithDict:(NSDictionary *)dic{
if (self=[super init]) {
self.title=dic[@"title"];
self.carGroupArray=dic[@"car"];
NSMutableArray *carArray=[NSMutableArray array];
for (NSDictionary *dic in _carGroupArray) {
CarModule *carModule=[CarModule carWithDict:dic];
[carArray addObject:carModule];
}
}
return self;
}
@end
接下來就是填充數(shù)據(jù)弃酌,這里用到了懶加載
#import "CarTableViewViewController.h"#import "CarGroup.h"#import "CarModule.h"@interface CarTableViewViewController()@end
@implementation CarTableViewViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//? ? [self menuData];
tableViewDemo=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/2-10) style:UITableViewStylePlain];
tableViewDemo.dataSource=self;
[[self view]addSubview:tableViewDemo];
}
//數(shù)據(jù)懶加載
-(NSMutableArray*)menuData{
if (!menuData) {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
NSArray *data=[NSArray arrayWithContentsOfFile:plistPath];
menuData=[NSMutableArray array];
for (NSDictionary *dic in data) {
CarGroup *carGroup=[CarGroup carGroupWithDict:dic];
[menuData addObject:carGroup];
}
}
return menuData;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.menuData.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
CarGroup *carGroup=self.menuData[section];
return carGroup.carGroupArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID=@"car";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
CarGroup *carGroup=self.menuData[indexPath.section];
CarModule *car=carGroup.carGroupArray[indexPath.row];
cell.imageView.image=[UIImage imageNamed:[car valueForKey:@"icon"]];
cell.textLabel.text=[car valueForKey:@"name"];
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
CarGroup *carGroup=self.menuData[section];
return carGroup.title;;
}
```