生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之汽车品牌项目-14
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之汽車品牌項目-14
#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
{
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
];}- (BOOL
)prefersStatusBarHidden
{return YES
;
}@end
#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
#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
#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
#import "CZGroup.h"
#import "CZCar.h"@implementation CZGroup
-(instancetype
)initWithDict
:(NSDictionary
*)dict
{if(self=[super init
]){
[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的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。