您的位置:首页 > 产品设计 > UI/UE

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



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: