[iOS基础控件 - 6.1] 汽车品牌列表 UITableView多项显示
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
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
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。