標籤:
//
// MJViewController.m
// 01-UITableView01-多組數組展示
//
// Created by apple on 13-11-28.
// Copyright (c) 2013年 itcast. All rights reserved.
//
#import "MJViewController.h"
#import "Province.h"
@interface MJViewController () <UITableViewDataSource>
{
NSArray *_allProvinces; // 所有的省份
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 1.添加tableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.dataSource = self;
[self.view addSubview:tableView];
// 2.初始化資料
_allProvinces = @[
[Province provinceWithHeader:@"廣東" footer:@"廣東好" cities:@[@"廣州", @"深圳"]],
[Province provinceWithHeader:@"湖南" footer:@"湖南也好" cities:@[@"長沙"]],
[Province provinceWithHeader:@"廣東2" footer:@"廣東好" cities:@[@"廣州", @"深圳"]],
[Province provinceWithHeader:@"湖南2" 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.citites.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.citites[indexPath.row];
return cell;
}
#pragma mark 第section組顯示的頭部標題
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// Province *province = _allProvinces[section];
//
//// return province.header;
// return [province header];
//}
#pragma mark 第section組顯示的尾部標題
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
// 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
UITableViewy基本使用小例子