UITableView 展示数据
1 // 2 // ViewController.m 3 // 13-UITableView 4 // 5 // Created by zjj on 15/5/14. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #define kCity @"Citys" 11 #define kProvies @"Provies" 12 #define kDescription @"Description" 13 @interface ViewController () <UITableViewDataSource> 14 { 15 NSArray *_allCicty; 16 } 17 @end 18 19 @implementation ViewController 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 UITableView *tabView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; 24 25 [self.view addSubview:tabView]; 26 tabView.dataSource = self; 27 28 _allCicty = @[ 29 @{ 30 kDescription :@"洛阳牡丹甲天下", 31 kProvies : @"河南", 32 kCity : @[@"洛阳",@"郑州",@"南阳",@"开封"] 33 }, 34 @{ 35 kDescription :@"我爱北京天安门", 36 kProvies : @"河北", 37 kCity : @[@"北京",@"天津"] 38 } 39 ]; 40 41 } 42 #pragma mark 分多少组 43 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 44 { 45 return _allCicty.count; 46 } 47 #pragma mark section组多少行(分行) 48 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 49 { 50 51 NSArray *ary = _allCicty[section][kCity]; 52 return ary.count; 53 } 54 #pragma mark 每行内容 55 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 56 { 57 UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 58 // NSArray *array =_allCicty[indexPath.section][kCity][indexPath.row]; 59 // NSString *text = array[indexPath.row]; 60 cell.textLabel.text = _allCicty[indexPath.section][kCity][indexPath.row]; 61 NSLog(@"%@",cell); 62 return cell; 63 } 64 #pragma mark 表头 第section组显示同头部标题 65 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 66 { 67 NSLog(@"%@",_allCicty[section][kProvies]); 68 return _allCicty[section][kProvies]; 69 } 70 #pragma mark 表尾 第section组显示尾部标题 71 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 72 { 73 return _allCicty[section][kDescription]; 74 } 75 @end
使用模型重构代码
pervies.h和.m模型类
1 // 2 // Provies.h 3 // 13-UITableView 4 // 5 // Created by zjj on 15/5/14. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #define kCity @"Citys" 11 #define kProvies @"Provies" 12 #define kDescription @"Description" 13 @interface Provies : NSObject 14 @property (nonatomic,copy)NSString *head; 15 @property (nonatomic,copy)NSString *foot; 16 @property (nonatomic,strong)NSArray *citys; 17 18 + (Provies *)proviesWithHead :(NSString *)head foot:(NSString *)foot citys :(NSArray *)citys; 19 @end
pervies.m 实现类
1 // 2 // Provies.m 3 // 13-UITableView 4 // 5 // Created by zjj on 15/5/14. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "Provies.h" 10 11 @implementation Provies 12 + (Provies *)proviesWithHead:(NSString *)head foot:(NSString *)foot citys:(NSArray *)citys 13 { 14 Provies *p = [[Provies alloc]init]; 15 p.head = head; 16 p.foot = foot; 17 p.citys = citys; 18 return p; 19 } 20 @end
初始化方法 手写控件
1 // 2 // ViewController.m 3 // 13-UITableView 4 // 5 // Created by zjj on 15/5/14. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 #import "Provies.h" 12 @interface ViewController () <UITableViewDataSource> 13 { 14 NSArray *_allCicty; 15 } 16 @end 17 18 @implementation ViewController 19 20 - (void)viewDidLoad { 21 [super viewDidLoad]; 22 UITableView *tabView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; 23 24 [self.view addSubview:tabView]; 25 tabView.dataSource = self; 26 27 Provies *proHN = [Provies proviesWithHead:@"河南" foot:@"洛阳牡丹甲天下" citys:@[@"洛阳",@"郑州",@"开封",@"许昌"]]; 28 Provies *proHB = [Provies proviesWithHead:@"河北" foot:@"我爱北京天安门" citys:@[@"北京",@"天津"]]; 29 30 _allCicty = @[proHN,proHB]; 31 } 32 #pragma mark 分多少组 33 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 34 { 35 return _allCicty.count; 36 } 37 #pragma mark section组多少行(分行) 38 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 39 { 40 Provies *provies = _allCicty[section]; 41 return provies.citys.count; 42 } 43 #pragma mark 每行内容 44 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 45 { 46 UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 47 cell.textLabel.text = [_allCicty[indexPath.section]citys][indexPath.row]; 48 return cell; 49 } 50 #pragma mark 表头 第section组显示同头部标题 51 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 52 { 53 return [_allCicty[section] head]; 54 } 55 #pragma mark 表尾 第section组显示尾部标题 56 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 57 { 58 return [_allCicty[section] foot]; 59 } 60 @end
加入右侧索引条
1 // 2 // ViewController.m 3 // 13-UITableView 4 // 5 // Created by zjj on 15/5/14. 6 // Copyright (c) 2015年 zjj. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 #import "Provies.h" 12 @interface ViewController () <UITableViewDataSource> 13 { 14 NSArray *_allCicty; 15 } 16 @end 17 18 @implementation ViewController 19 20 - (void)viewDidLoad { 21 [super viewDidLoad]; 22 UITableView *tabView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; 23 24 [self.view addSubview:tabView]; 25 tabView.dataSource = self; 26 27 _allCicty = @[ 28 [Provies proviesWithHead:@"河南" foot:@"洛阳牡丹甲天下" citys:@[@"洛阳",@"郑州",@"开封",@"许昌"]] 29 ,[Provies proviesWithHead:@"河北" foot:@"我爱北京天安门" citys:@[@"北京",@"天津"]] 30 ,[Provies proviesWithHead:@"湖南" foot:@"湖南辣椒" citys:@[@"长沙",@"岳阳",@"黄冈"]] 31 , [Provies proviesWithHead:@"山西" foot:@"山西老陈醋" citys:@[@"太原",@"大同",@"长治",@"运城"]] 32 , [Provies proviesWithHead:@"青海" foot:@"青海湖" citys:@[@"青海"]] 33 ]; 34 } 35 #pragma mark 分多少组 36 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 37 { 38 return _allCicty.count; 39 } 40 #pragma mark section组多少行(分行) 41 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 42 { 43 Provies *provies = _allCicty[section]; 44 return provies.citys.count; 45 } 46 #pragma mark 每行内容 47 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 48 { 49 UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; 50 cell.textLabel.text = [_allCicty[indexPath.section]citys][indexPath.row]; 51 return cell; 52 } 53 #pragma mark 表头 第section组显示同头部标题 54 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 55 { 56 return [_allCicty[section] head]; 57 } 58 #pragma mark 表尾 第section组显示尾部标题 59 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 60 { 61 return [_allCicty[section] foot]; 62 } 63 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 64 { 65 // NSMutableArray *titles = [NSMutableArray array]; 66 // for (Provies *p in _allCicty) { 67 // [titles addObject:p.head]; 68 //// [titles valueForKey:p.head];// for in实现 69 // } 70 // return titles; 71 72 NSMutableArray *titles = [NSMutableArray array]; 73 for (int i = 0; i<_allCicty.count; i++) { 74 Provies *p = _allCicty[i]; 75 [titles addObject:[p valueForKey:@"head"]];//kvc-kvo方法实现 取出某一类对象同一个属性 76 } 77 return titles; 78 } 79 @end
右侧蓝色省份索引条