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>
/**
* 車品牌資料
*/
@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) {
// 初始化
// 1.獲得plist的全路徑
NSString *path = [[NSBundlemainBundle] pathForResource:@"cars_total.plist"ofType:nil];
// 2.載入數組
NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];
// 3.將dictArray裡面的所有字典轉成模型對象,放到新的數組中
NSMutableArray *carGroupsArray = [NSMutableArrayarray];
for (NSDictionary *dictin dictArray) {
// 3.1.建立模型對象
CarGroup *group = [CarGroupgroupWithDict:dict];
// 3.2.添加模型對象到數組中
[carGroupsArrayaddObject:group];
}
// 4.賦值
_carGroups = carGroupsArray;
}
return_carGroups;
}
#pragma mark -資料來源模型
-(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
{
//定義一個迴圈標識
static NSString * ID =@"car";
//2.從緩衝池中取出可迴圈利用的cell
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier:ID];
//3.如果緩衝池中沒有那麼就要建立一個 並且要賦予標識
if (cell == nil)
{
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
}
//4.設定資料
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;
}
/**
* 第section組顯示的頭部標題
*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
CarGroup * group = self.carGroups[section];
return group.title;
}
/**
* 返回回右邊索引跳顯示的字串資料
*/
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [self.carGroupsvalueForKeyPath:@"title"];//這是KVC可以間接取值
//為什麼不用[self.carGroups valueForKey:@"title"]因為這個不會間接取值
}
@end