UITableView-多组数组展示
2014-06-02 17:49
381 查看
@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
相关文章推荐
- UITableView实现多组汽车展示(模型里面嵌套模型)
- IOS开发UI展示之UITableView ──分页加载
- Core Data浅谈系列之五 : 在UITableView中展示
- ios开发——用UITableView展示多行多列图片
- UI基础第十四弹:实现UItableview使用嵌套模型完成的一个简单汽车图标展示程序
- 【iOS基础控件 6 】 汽车品牌展示 Model嵌套/KVC/TableView索引 <UITableView>
- IOS开发UI展示之UITableView ──分页加载
- IOS UITableView 实现LOL数据展示
- UITableView 数组与字典结合(省市区)
- UITableView 数据展示 cell
- (十八)TableView实践(多组汽车品牌展示)
- IOS学习笔记(10)UITableView展示数据 cell删除 构建页眉页脚
- UITableView——实现汽车分组展示
- 在UIViewController里用UITableView及相关的tableHeaderView、tableFooterView实现三级连动页面页面展示效果
- Core Data浅谈系列之五 : 在UITableView中展示
- IOS开发UI展示之UITableView ──分页加载
- IOS开发UI展示之UITableView ──分页加载更多
- Core Data浅谈系列之五 : 在UITableView中展示
- ios开发——用UITableView展示多行多列图片
- (十八)TableView实践(多组汽车品牌展示)