ios 汽车品牌展示案例

时间:2023-03-09 17:07:28
ios 汽车品牌展示案例

ios 汽车品牌展示案例

汽车组模型

//  ZQRGroup.h
#import <Foundation/Foundation.h> @interface ZQRGroup : NSObject
/**
*组标题
*/
@property (nonatomic,copy) NSString *title;
/**
*品牌汽车
*/
@property (nonatomic,strong) NSArray *cars; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)groupWithDict:(NSDictionary *)dict;
@end //
// ZQRGroup.m
//
#import "ZQRGroup.h"
#import "ZQRCar.h" @implementation ZQRGroup - (instancetype)initWithDict:(NSDictionary *)dict
{
if(self=[super init]){
self.title=dict[@"title"];
NSArray *dictArray=dict[@"cars"];
NSMutableArray *carArray=[NSMutableArray array];
for (NSDictionary *dict in dictArray) {
ZQRCar *car=[[ZQRCar alloc] initWithDict:dict];
[carArray addObject:car];
}
self.cars=carArray;
}
return self;
}
+ (instancetype)groupWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end

汽车模型

//
// ZQRCar.h
//
#import <Foundation/Foundation.h>
@interface ZQRCar : NSObject
/**
*图标
*/
@property (nonatomic,copy) NSString *icon;
/**
*名称
*/
@property (nonatomic,copy) NSString *name; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carWithDict:(NSDictionary *)dict;
@end //
// ZQRCar.m
//
#import "ZQRCar.h"
@implementation ZQRCar
- (instancetype)initWithDict:(NSDictionary *)dict
{
if(self=[super init]){
//self.icon=dict[@"icon"];
//self.name=dict[@"name"];
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end

主控制器

//
// ZQRViewController.m
//
#import "ZQRViewController.h"
#import "ZQRGroup.h"
#import "ZQRCar.h" @interface ZQRViewController ()<UITableViewDataSource>
/**
* 车品牌组数据
*/
@property (nonatomic, strong) NSArray *groups;
@end @implementation ZQRViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (NSArray *)groups
{
if (_groups == nil) {
// 初始化
// 1.获得plist的全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]; // 2.加载数组
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
NSMutableArray *groupArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// 3.1.创建模型对象
ZQRGroup *group = [ZQRGroup groupWithDict:dict]; // 3.2.添加模型对象到数组中
[groupArray addObject:group];
} // 4.赋值
_groups = groupArray;
}
return _groups;
} #pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ZQRGroup *group = self.groups[section]; return group.cars.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.定义一个循环标识
static NSString *ID = @"car"; // 2.从缓存池中取出可循环利用cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3.缓存池中没有可循环利用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 4.设置数据
ZQRGroup *group = self.groups[indexPath.section];
ZQRCar *car = group.cars[indexPath.row]; cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name; return cell;
} /**
* 第section组显示的头部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
ZQRGroup *group = self.groups[section];
return group.title;
} /**
* 返回右边索引条显示的字符串数据
*/
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.groups valueForKeyPath:@"title"];
} - (BOOL)prefersStatusBarHidden
{
return YES;
}
@end