35.小项目:汽车列表 版本3.0
更新说明:使用plist数据,隐藏电池状态栏等
------------- ViewController.m -------------
#import "ViewController.h"
#import "CZCarGroup.h"
#import "CZCar.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,strong) NSArray *carGroups;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (NSArray *)carGroups
{
if (_carGroups == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
NSArray *groupDictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *groupDict in groupDictArray)
{
CZCarGroup *carGroup = [CZCarGroup carGroupWithDict:groupDict];
[tmpArray addObject:carGroup];
}
_carGroups = tmpArray;
}
return _carGroups;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carGroups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CZCarGroup *carG = self.carGroups[section];
return carG.cars.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];
}
CZCarGroup *carGroup = self.carGroups[indexPath.section];
CZCar *car = carGroup.cars[indexPath.row];
cell.textLabel.text = car.name;
cell.imageView.image = [UIImage imageNamed:car.icon];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CZCarGroup *carGroup = self.carGroups[section];
return carGroup.title;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.carGroups valueForKeyPath:@"title"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
@end
------------- CZCarGroup.h -------------
#import <Foundation/Foundation.h>
@interface CZCarGroup : NSObject
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) NSArray *cars;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carGroupWithDict:(NSDictionary *)dict;
@end
------------- CZCarGroup.m -------------
#import "CZCarGroup.h"
#import "CZCar.h"
@implementation CZCarGroup
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init])
{
self.title = dict[@"title"];
NSArray *dictArray = dict[@"cars"];
NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray)
{
CZCar *car = [CZCar carWithDict:dict];
[tmpArray addObject:car];
}
self.cars = tmpArray;
}
return self;
}
+ (instancetype)carGroupWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
------------- CZCar.h -------------
#import <Foundation/Foundation.h>
@interface CZCar : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carWithDict:(NSDictionary *)dict;
@end
------------- CZCar.m -------------
#import "CZCar.h"
@implementation CZCar
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init])
{
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end