汽车品牌案例:

本文介绍了一款iOS应用的设计与实现,该应用通过解析plist文件加载汽车品牌数据,并使用自定义的数据结构来组织这些信息。文章详细展示了如何创建Car和CarGroup两个类,以及如何在ViewController中初始化并展示这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

汽车品牌案例:

数据结构:
#import <Foundation/Foundation.h>

@interface Car : NSObject

@property(nonatomic, copy) NSString *icon;//图标
@property(nonatomic, copy) NSString *name;//名称

+ (instancetype)carWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;

@end

#import "Car.h"

@implementation Car

+ (instancetype)carWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

@end

#import <Foundation/Foundation.h>

@interface CarGroup : NSObject

@property(nonatomic, copy) NSString *title;//标题
@property(nonatomic, strong) NSArray *cars;//存放所有汽车品牌

+ (instancetype)groupWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;

@end

#import "CarGroup.h"
#import "Car.h"

@implementation CarGroup

+ (instancetype)groupWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (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) {
            Car *car = [Car carWithDict:dict];
            [carArray addObject:car];
        }
        self.cars = carArray;
    }
    
    return self;
}

@end
--------------------------------------------------------------------------------------------------------

#import "ViewController.h"
#import "CarGroup.h"
#import "Car.h"

@interface ViewController () <UITableViewDataSource>

@property(nonatomic, strong) NSArray *groups;//所有车品牌数据

@end

@implementation ViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (NSArray *)groups
{
    if (_groups == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *groupsArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            CarGroup *group = [CarGroup groupWithDict:dict];
            [groupsArray addObject:group];
        }
        _groups = groupsArray;
    }
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma  mark - 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

#pragma mark - 多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CarGroup *group = self.groups[section];
    return group.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cargroup";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    CarGroup *group = self.groups[indexPath.section];
    Car *car = group.cars[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.textLabel.text = car.name;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CarGroup *group = self.groups[section];
    return group.title;
}

/**
 返回索引栏的数据(右边索引条显示的ABCDE)
 原理:这边对应的数组的索引对应左边显示的nstableview的索引
 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.groups valueForKeyPath:@"title"];
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值