日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

IOS开发基础之汽车品牌项目-14

發布時間:2023/12/18 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之汽车品牌项目-14 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOS開發基礎之汽車品牌項目-14



// // ViewController.m // 16-汽車品牌展示02 // // Created by 魯軍 on 2021/2/3. //#import "ViewController.h" #import "CZGroup.h" #import "CZCar.h" @interface ViewController () <UITableViewDataSource,UITableViewDelegate>@property(nonatomic,strong)NSArray *groups;@end@implementation ViewController- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ // // NSMutableArray *arrayIndex = [NSMutableArray array]; // for(CZGroup *group in self.groups){ // [arrayIndex addObject:group.title]; // } // return arrayIndex;// return @[@"A",@"B",@"C",@"D"];return [self.groups valueForKey:@"title"];}#pragma 數據源方法- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{CZGroup *group = self.groups[section];return group.title;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{CZGroup *group = self.groups[indexPath.section];CZCar *car = group.cars[indexPath.row];static NSString *ID = @"car_cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];if(cell==nil){cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];}cell.imageView.image = [UIImage imageNamed:car.icon];cell.textLabel.text=car.name;return cell;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{CZGroup *group = self.groups[section];return group.cars.count; }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return self.groups.count; }#pragma mark - 懶加載數據- (NSArray *)groups{if(_groups==nil){NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];NSArray *arrayDict=[NSArray arrayWithContentsOfFile:path];NSMutableArray *arrayModels = [NSMutableArray array];for(NSDictionary *dict in arrayDict){CZGroup *model = [CZGroup groupWithDict:dict];[arrayModels addObject:model];}_groups = arrayModels;}return _groups;}- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.}- (BOOL)prefersStatusBarHidden{return YES; }@end // // CZCar.h // 16-汽車品牌展示02 // // Created by 魯軍 on 2021/2/3. //#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface CZCar : NSObject@property(nonatomic,copy)NSString *icon; @property(nonatomic,copy)NSString *name;-(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)carWithDict:(NSDictionary *)dict;@endNS_ASSUME_NONNULL_END // // CZCar.m // 16-汽車品牌展示02 // // Created by 魯軍 on 2021/2/3. //#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 // // CZGroup.h // 16-汽車品牌展示02 // // Created by 魯軍 on 2021/2/3. //#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface CZGroup : NSObject @property(nonatomic,copy)NSString *title; @property(nonatomic,strong)NSArray *cars;-(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)groupWithDict:(NSDictionary *)dict;@endNS_ASSUME_NONNULL_END // // CZGroup.m // 16-汽車品牌展示02 // // Created by 魯軍 on 2021/2/3. //#import "CZGroup.h" #import "CZCar.h"@implementation CZGroup -(instancetype)initWithDict:(NSDictionary *)dict{if(self=[super init]){ // self.title = dict[@"title"]; // self.cars=dict[@"cars"];[self setValuesForKeysWithDictionary:dict];//創建一個用來保存模型的數組NSMutableArray *arrayModels = [NSMutableArray array];//當有模型嵌套的時候需要注意的地方//手動的做一下字典轉模型for(NSDictionary *item_dict in dict[@"cars"]){CZCar *model = [CZCar carWithDict:item_dict];[arrayModels addObject:model];}self.cars = arrayModels;}return self;} +(instancetype)groupWithDict:(NSDictionary *)dict{return [[self alloc] initWithDict:dict];}@end

總結

以上是生活随笔為你收集整理的IOS开发基础之汽车品牌项目-14的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。