• iOS-汽车品牌app


    资料:汽车图片 和plist

    文件:两个模型

    //
    //  Car.h
    //  汽车品牌
    //
    //  Created by YaguangZhu on 15/8/16.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Car : NSObject
    
    @property(nonatomic,copy)NSString *name;
    
    @property(nonatomic,copy)NSString *icon;
    
    - (instancetype)initWithDict:(NSDictionary *)dict;
    + (instancetype)carWithDict:(NSDictionary *)dict;
    + (NSArray *)carsWithArray:(NSArray *)array;
    
    
    
    
    @end
    //
    //  Car.m
    //  汽车品牌
    //
    //  Created by YaguangZhu on 15/8/16.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "Car.h"
    
    @implementation Car
    
    - (instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
            [self setValuesForKeysWithDictionary:dict];
        }
        return self;
    }
    
    + (instancetype)carWithDict:(NSDictionary *)dict
    {
        return [[self alloc] initWithDict:dict];
    }
    
    + (NSArray *)carsWithArray:(NSArray *)array
    {
        //NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [arrayM addObject:[self carWithDict:dict]];
        }
        
        return arrayM;
    }
    
    @end
    //
    //  carGroup.m
    //  汽车品牌
    //
    //  Created by YaguangZhu on 15/8/16.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "carGroup.h"
    #import "Car.h"
    @implementation carGroup
    
    - (instancetype)initWithDict:(NSDictionary *)dict
    {
        self = [super init];
        if (self) {
    //[self setValuesForKeysWithDictionary:dict];
            [self setValue:dict[@"title"] forKey:@"title"];
            self.cars = [Car carsWithArray:dict[@"cars"]];
        }
        return self;
    }
    
    + (instancetype)carGroupWithDict:(NSDictionary *)dict
    {
        return [[self alloc] initWithDict:dict];
    }
    
    + (NSArray *)carGroups1
    {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]];
        NSMutableArray *arrayM = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [arrayM addObject:[self carGroupWithDict:dict]];
        }
        
        return arrayM;
    }
    
    
    @end
    //
    //  carGroup.h
    //  汽车品牌
    //
    //  Created by YaguangZhu on 15/8/16.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface carGroup : NSObject
    @property(nonatomic,copy)NSString *title;
    @property(nonatomic,strong)NSArray *cars;
    
    - (instancetype)initWithDict:(NSDictionary *)dict;
    + (instancetype)carGroupWithDict:(NSDictionary *)dict;
    + (NSArray *)carGroups1;
    
    @end
    //
    //  ViewController.m
    //  汽车品牌
    //
    //  Created by YaguangZhu on 15/8/16.
    //  Copyright (c) 2015年 YaguangZhu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "carGroup.h"
    #import "Car.h"
    @interface ViewController ()<UITableViewDataSource>
    @property(nonatomic,strong)NSArray *carGroups1;
    @property(nonatomic,strong)UITableView *tableView;
    @end
    
    @implementation ViewController
    - (UITableView *)tableView
    {
        if (_tableView == nil) {
            _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
            _tableView.dataSource = self;
            
            [self.view addSubview:_tableView];
        }
        return _tableView;
    }
    - (NSArray *)carGroups1
    {
        if (_carGroups1 == nil) {
            _carGroups1 = [carGroup carGroups1];
        }
        
        return _carGroups1;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
       // NSLog(@"%@",self.carGroups1);
        [self tableView];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    //数据源方法
    //分组的总数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return self.carGroups1.count;
    }
    
    //每一组的总数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        carGroup *group = self.carGroups1[section];
        
        return group.cars.count;
    }
    
    //单元格
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell ==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        carGroup *group = self.carGroups1[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 titleForFooterInSection:(NSInteger)section
    {
        carGroup *group = self.carGroups1[section];
        return group.title;
    }
    
    //右侧索引列表
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
       /* NSMutableArray *arrayM = [NSMutableArray array];
        for (carGroup *group in self.carGroups1) {
            [arrayM addObject:group.title];
        }
        return arrayM;*/
        
        
        //kvc 是cocoa 的大招
        
        return [self.carGroups1 valueForKeyPath:@"title"];
    }
    
    
    
    @end
  • 相关阅读:
    C++实现反射
    ubuntu下安装secureCRT(含破解方法)
    2018 年力扣高频算法面试题汇总-难题记录-鸡蛋掉落
    对于opencv全面貌的认识和理解
    关于c++类的一些知识的总结
    vs2017+opencv4.0.1安装配置详解(win10)
    leetcode-120-三角形最小路径和
    leetcode-64-最小路径和
    leetcode-917-仅仅反转字母
    leetcode-914-卡牌分组
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4734321.html
Copyright © 2020-2023  润新知