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 

转载于:https://www.cnblogs.com/lixiang2015/p/4709632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值