UITableView

//

//  ViewController.m

//  UITableView分组展示数据

//

//  Created by lavieenrose on 14/11/24.

//  Copyright (c) 2014年 lavieenrose. All rights reserved.

//

#import “ViewController.h”

#define HEADER @”header”

#define FOOTER @”footer”

#define CITIES @”cities”

@interface ViewController (){

NSArray *_allProvinces;

}

@end

@implementation ViewController

– (void)viewDidLoad {

[super viewDidLoad];

UITableView *tv = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

CGRect frame = tv.frame;

frame.origin.y = 20;

tv.frame = frame;

tv.dataSource = self;

[self.view addSubview:tv];

_allProvinces = @[

@{

HEADER:@”广东”,

FOOTER:@”广东好”,

CITIES:@[@”广州”,@”深圳”,@”没船”]

},

@{

HEADER:@”浙江”,

FOOTER:@”浙江好”,

CITIES:@[@”嘉兴”,@”平湖”]

},

@{

HEADER:@”湖北”,

FOOTER:@”湖北好”,

CITIES:@[@”武汉”,@”黄冈”]

}

];

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

NSDictionary *province = _allProvinces[indexPath.section];

NSArray *cities = province[CITIES];

NSString *text = cities[indexPath.row];

cell.textLabel.text = text;

return cell;

}

#pragma mark 组的底部文字

– (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return _allProvinces[section][FOOTER];

}

#pragma mark 组的头文字

– (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return _allProvinces[section][HEADER];

}

#pragma mark -数据源

#pragma mark 一共多少个省份

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return _allProvinces.count;

}

#pragma mark 组里有多少行

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 1;

NSDictionary *province = _allProvinces[section];

NSArray *cities = province[CITIES];

return cities.count;

}

– (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


发表评论

电子邮件地址不会被公开。 必填项已用*标注