UITableView-多组数组展示
来源:互联网 发布:手机如何换成2g网络 编辑:程序博客网 时间:2024/05/20 20:57
@interface Province : NSObject// UI控件用weak,NSString用copy, 其他对象一般用strong@property(nonatomic,copy) NSString *header;@property(nonatomic,copy) NSString *footer;@property(nonatomic,strong) NSArray *cities;+ (id)provinceWithHeader:(NSString *)header footer:(NSString *)footer cities:(NSArray *)cities;@end
@implementation Province+(id)provinceWithHeader:(NSString *)header footer:(NSString *)footer cities:(NSArray *)cities{ Province *p = [[Province alloc]init]; p.header = header; p.footer = footer; p.cities = cities; return p;}@end
<pre name="code" class="objc">#import "ViewController.h"#import "Province.h"@interface ViewController () <UITableViewDataSource>{ NSArray *_allProvinces;// 所有省}@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // 1.添加tableView UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; tableView.dataSource = self;// 设置数据源 [self.view addSubview:tableView]; // 2.初始化数据 // (模型对象,数据模型,Model,仅仅是用来存放数据的对象,<属性>) _allProvinces = @[ [Province provinceWithHeader:@"广东" footer:@"粤" cities:@[@"广州",@"深圳",@"梅州",@"东莞",]], [Province provinceWithHeader:@"湖南" footer:@"湘" cities:@[@"长沙",@"益阳",@"岳阳",@"常德",@"邵阳"]], [Province provinceWithHeader:@"湖北" footer:@"鄂" cities:@[@"武汉",@"黄冈"]], [Province provinceWithHeader:@"广西" footer:@"桂" cities:@[@"桂林"]], ];}#pragma mark - 数据源方法#pragma mark - 一共有多少组 (section == 区域/组)-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _allProvinces.count;}#pragma mark 第section组一共有多少行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 1.取得第section组的省份 Province *province = _allProvinces[section]; // 2.取得省份里面的城市数组 return province.cities.count;}#pragma mark 返回每一行显示的内容 (每一行显示怎样的cell)-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; Province *province = _allProvinces[indexPath.section]; cell.textLabel.text = province.cities[indexPath.row]; return cell;}#pragma mark 第section组显示的头部标题-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ Province *province = _allProvinces[section]; return province.header;}#pragma mark 第section组显示的尾部标题-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{// _allProvinces[section]; // 等于下面这行// [_allProvinces objectAtIndex:section]; 返回id,id类型不能用点语法 return [_allProvinces[section]footer];}#pragma mark 返回表格右边现实的索引条-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ NSMutableArray *titles = [NSMutableArray array]; for (Province *p in _allProvinces) { [titles addObject:p.header]; } return titles;}@end
0 0
- UITableView-多组数组展示
- UITableView 多个分区的展示
- UI 一一 UITableView多组和单组数据展示案例
- UITableView实现多组汽车展示(模型里面嵌套模型)
- UITableView展示ModuleViewController
- UITableView展示ModuleViewController
- UITableView展示ModuleViewController
- UITableView 数据展示
- UITableView 数据展示 cell
- UITableView之LOL英雄展示
- UITableView——实现汽车分组展示
- IOS UITableView 实现LOL数据展示
- 黑马程序员------UITableView数据的展示
- iOS程序-UITableView分组展示数据
- UITableView省市区数组
- UITableView 数组越位
- UITableView 数组越位
- UITableView 数组越位
- 黑马程序员_java的IO流五_18
- 普林斯顿公开课 算法1-11:并查集的应用
- TFS 2010 迁移/重装/还原 步骤
- OCP 1Z0 051 QUESTION NO: 11
- 应该记住的基本流程及其函数、背面剔除
- UITableView-多组数组展示
- webbrowser局部刷新完成的问题
- ios 中生成随机数
- 普林斯顿公开课 算法2-1:排序概述
- android下OnTouchListener
- 普林斯顿公开课 算法2-2:选择排序
- JAVA的一次编译,到处运行,你知道多少?
- uva--409-- Excuses, Excuses!
- 工资数据的输入