ViewController UITableView, viewcontroller
//
// ViewController. m
// CarDemo
//
// Created by GuoYule on 15/1/20.
// Copyright (c) 2015 GuoYule. All rights reserved.
//
# Import "ViewController. h"
# Import "CarGroup. h"
# Import "GYLcar. h"
@ Interface ViewController () <UITableViewDataSource>
/**
* Vehicle brand data
*/
@ Property (nonatomic, strong) NSArray * carGroups;
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[SuperviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(BOOL) prefersStatusBarHidden
{
Return YES;
}
-(NSArray *) carGroups
{
If (_ carGroups = nil ){
// Initialization
// 1. Obtain the full path of plist
NSString * path = [[NSBundlemainBundle] pathForResource: @ "cars_total.plist" ofType: nil];
// 2. Load the Array
NSArray * dictArray = [NSArrayarrayWithContentsOfFile: path];
// 3. convert all the dictionaries in dictArray into model objects and place them in the new array.
NSMutableArray * carGroupsArray = [NSMutableArrayarray];
For (NSDictionary * dictin dictArray ){
// 3. 1. Create a model object
CarGroup * group = [CarGroupgroupWithDict: dict];
//. Add the model object to the array
[CarGroupsArrayaddObject: group];
}
// 4. assign a value
_ CarGroups = carGroupsArray;
}
Return_carGroups;
}
# Pragma mark-Data Source Model
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
{
Return self. carGroups. count;
}
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
CarGroup * group = self. carGroups [section];
Return group. cars. count;
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
// Define a loop ID
Static NSString * ID = @ "car ";
// 2. Fetch reusable cells from the cache pool
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier: ID];
// 3. If the cache pool does not exist, create a new one and assign the ID to it.
If (cell = nil)
{
Cell = [[UITableViewCellalloc] initWithStyle: UITableViewCellStyleDefaultreuseIdentifier: ID];
}
// 4. Set Data
CarGroup * group = self. carGroups [indexPath. section];
GYLcar * car = group. cars [indexPath. row];
Cell. imageView. image = [UIImageimageNamed: car. icon];
Cell. textLabel. text = car. name;
Return cell;
}
/**
* Header title displayed in section group
*/
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section
{
CarGroup * group = self. carGroups [section];
Return group. title;
}
/**
* Returns the string data displayed in the right index hop.
*/
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {
Return [self. carGroupsvalueForKeyPath: @ "title"]; // This is an indirect value that KVC can take.
// Why not use [self. carGroups valueForKey: @ "title"] because this value is not indirect
}
@ End