A.实现思路
1.拖入UITableView
2.拖曳、连线UITableView控件
3.Controller遵守UITalbeViewDataSource协议
4.设置UITableView的dataSource
5.加载数据到Model
6.从Model解析数据,显示到View上
B.实现细节
1.UITableView style
(1)Grouped,成组出现,标题和尾部会被分隔开,如上图
(2)Plain
2. cars_simple.plist 文件结构
C.主要代码
Car.h
1 // 2 // Car.h 3 // 01-CarBrand 4 // 5 // Created by hellovoidworld on 14/11/30. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface Car : NSObject 12 13 @property(nonatomic, strong) NSArray *cars; 14 @property(nonatomic, copy) NSString *title; 15 @property(nonatomic, copy) NSString *desc; 16 17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary; 18 + (instancetype) carWithDictionary:(NSDictionary *) dictionary; 19 + (instancetype) car; 20 21 @end
Car.m
1 // 2 // Car.m 3 // 01-CarBrand 4 // 5 // Created by hellovoidworld on 14/11/30. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "Car.h" 10 11 @implementation Car 12 13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 14 if (self == [super init]) { 15 self.cars = dictionary[@"cars"]; 16 self.title = dictionary[@"title"]; 17 self.desc = dictionary[@"desc"]; 18 } 19 20 return self; 21 } 22 23 + (instancetype) carWithDictionary:(NSDictionary *) dictionary { 24 return [[self alloc] initWithDictionary:dictionary]; 25 } 26 27 + (instancetype) car { 28 return [self carWithDictionary:nil]; 29 } 30 31 @end
ViewController.m
1 // 2 // ViewController.m 3 // 01-CarBrand 4 // 5 // Created by hellovoidworld on 14/11/30. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Car.h" 11 12 @interface ViewController () <UITableViewDataSource> 13 14 @property (weak, nonatomic) IBOutlet UITableView *tableView; 15 16 @property(nonatomic, strong) NSArray *allBrandOfCars; 17 18 @end 19 20 @implementation ViewController 21 22 - (void)viewDidLoad { 23 [super viewDidLoad]; 24 // Do any additional setup after loading the view, typically from a nib. 25 26 self.tableView.dataSource = self; 27 } 28 29 - (void)didReceiveMemoryWarning { 30 [super didReceiveMemoryWarning]; 31 // Dispose of any resources that can be recreated. 32 } 33 34 35 #pragma mark - dataSource方法 36 37 /** Sections 数,组数 */ 38 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 39 return self.allBrandOfCars.count; // 所有车的派系的数量 40 } 41 42 /** 组内的行数 */ 43 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 44 Car *car = self.allBrandOfCars[section]; 45 return car.cars.count; // 每个派系的车的牌子的数量 46 } 47 48 /** 组的标题 49 这里是车的派系 50 */ 51 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 52 Car *car = self.allBrandOfCars[section]; 53 return car.title; 54 } 55 56 /** 组的尾部 */ 57 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { 58 Car *car = self.allBrandOfCars[section]; 59 return car.desc; 60 } 61 62 /** 行的内容 */ 63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 64 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 65 66 Car *car = self.allBrandOfCars[indexPath.section]; // 车的派系 67 NSString *carName = car.cars[indexPath.row]; // 具体车的牌子 68 69 cell.textLabel.text = carName; 70 71 return cell; 72 } 73 74 75 /** 延迟加载plist文件中的数据 */ 76 - (NSArray *) allBrandOfCars { 77 if (nil == _allBrandOfCars) { 78 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_simple" ofType:@"plist"]]; 79 80 NSMutableArray *mdictArray = [NSMutableArray array]; 81 for (NSDictionary *dict in dictArray) { 82 Car *car = [Car carWithDictionary:dict]; 83 [mdictArray addObject:car]; 84 } 85 86 _allBrandOfCars = mdictArray; 87 } 88 89 return _allBrandOfCars; 90 } 91 92 93 @end