TableView多汽车数据,模型嵌套

本文介绍了一个iOS应用案例,该应用使用UITableView展示从plist文件加载的多组汽车数据。通过CZCarGroup和CZCar两个模型类实现了数据的解析与展示。

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

//
//  ViewController.m
//  05展示 多汽车数据
//
//  Created by Itcast.GuangZhou on 15/9/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "ViewController.h"
#import "CZCar.h"
#import "CZCarGroup.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//模型数组
@property(nonatomic,strong) NSArray *cars;

@end

@implementation ViewController

- (NSArray *)cars
{
    if (_cars==nil) {
        _cars=[CZCarGroup carGroups];
    }
    return _cars;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
}
//确定组的数量,如果不实现默认为1组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.cars.count;
}

//确定组中一共有几行数据
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CZCarGroup *cargroup=self.cars[section];
    return cargroup.cars.count;
}

//创建每一行显示的cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ID=@"car";
    //1.创建cell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //获取 模型
    //先取出组
    CZCarGroup *carGroup=self.cars[indexPath.section];
    //再取出这个组中的某个car模型
    CZCar *car=carGroup.cars[indexPath.row];
    //2.为cell赋值
    cell.imageView.image=[UIImage imageNamed:car.icon];
    cell.textLabel.text=car.name;
    //返回
    return  cell;
}
//设置每一个组的头部标题
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //先取出组
    CZCarGroup *carGroup=self.cars[section];

    return  carGroup.title;
}

//显示组导航
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *arr=[NSMutableArray array];
    for (CZCarGroup *group in self.cars) {
        [arr addObject:group.title];
    }
    return arr;
}


@end



<pre name="code" class="objc">//
//  CZCar.h
//  05展示 多汽车数据
//
//  Created by Itcast.GuangZhou on 15/9/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CZCar : NSObject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *name;
- (instancetype) initWithDic:(NSDictionary *)dic;
+ (instancetype) carWithDic:(NSDictionary *)dic;

//接收一个字典数组,返回一个模型数组
+ (NSArray *) cars:(NSArray *)dicArr;
@end




<pre name="code" class="objc">//
//  CZCar.m
//  05展示 多汽车数据
//
//  Created by Itcast.GuangZhou on 15/9/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZCar.h"

@implementation CZCar
- (instancetype)initWithDic:(NSDictionary *)dic
{
    if(self=[super init])
    {
        [self setValuesForKeysWithDictionary:dic];
    }
    return  self;
}
+ (instancetype)carWithDic:(NSDictionary *)dic
{
    return  [[self alloc] initWithDic:dic];
}
//通过字典数组转换模型数组
+ (NSArray *)cars:(NSArray *)dicArr
{
    NSMutableArray *desArr=[NSMutableArray array];
    
    for (NSDictionary *dic in dicArr) {
        CZCar *car=[CZCar carWithDic:dic];
        [desArr addObject:car];
    }
    return  desArr;
}

@end




<pre name="code" class="objc">//
//  CZCarGroup.h
//  05展示 多汽车数据
//
//  Created by Itcast.GuangZhou on 15/9/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CZCarGroup : NSObject
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) NSArray *cars;


- (instancetype) initWithDic:(NSDictionary *)dic;
+ (instancetype) carGroupWithDic:(NSDictionary *)dic;
+ (NSArray *) carGroups;
@end




<pre name="code" class="objc">//
//  CZCarGroup.m
//  05展示 多汽车数据
//
//  Created by Itcast.GuangZhou on 15/9/19.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZCarGroup.h"
#import "CZCar.h"

@implementation CZCarGroup
- (instancetype)initWithDic:(NSDictionary *)dic
{
    if (self=[super init]) {
        self.title=dic[@"title"];
        //dic[@"cars"]只是得到一个字典数组,里面的每一个item项还是一个字典,我们还需要对其再次进行转换
//        NSMutableArray *arr=[NSMutableArray array];
//        //self.cars=dic[@"cars"];
//        for (NSDictionary *subDic in dic[@"cars"]) {
//            CZCar *car=[CZCar carWithDic:subDic];
//            [arr addObject:car];
//        }
//        self.cars=arr;
        self.cars=[CZCar cars:dic[@"cars"]];
    }
    return  self;
}

+ (instancetype)carGroupWithDic:(NSDictionary *)dic
{
    return [[self alloc] initWithDic:dic];
}

+ (NSArray *) carGroups
{
    NSArray *sourceArrr=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
    
    NSMutableArray *desArr=[NSMutableArray array];
    
    for (NSDictionary *dic in sourceArrr) {
        CZCarGroup *cg=[CZCarGroup carGroupWithDic:dic];
        [desArr addObject:cg];
    }
    return desArr;
}
@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值