#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>{
NSArray *gdCities;
NSArray *hnCities;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[color=red][b]//1.添加tableview[/b][/color]
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
[self.view addSubview:tableView];
//2.初始化数据
gdCities = @[@"广东", @"深圳", @"梅州", @"东莞"];
hnCities = @[@"长沙", @"岳阳", @"邵阳", @"益阳"];
}
#pragma mark - 数据源方法
#pragma mark - 一共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
[color=green][b]#pragma mark - 一共有多少行[/b][/color]
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){//广东
// return 4;
return gdCities.count;
}else{//湖南
// return 2;
return hnCities.count;
}
}
[color=darkblue][b]#pragma mark - 返回每行显示的内容[/b][/color]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//indexpath 标示唯一的一行
// indexPath.section == 0;
// indexPath.row == 0;
[color=darkred]UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];[/color]
//设置cell显示的文字
NSString *text = nil;
if(indexPath.section == 0){//广东
// if(indexPath.row == 0){
// text = @"广州";
// }else if(indexPath.row == 1){
// text = @"深圳";
// }else if (indexPath.row == 2){
// text = @"梅州";
// }else if (indexPath.row == 3){
// text = @"东莞";
// }
text = [gdCities objectAtIndex:indexPath.row];
}else{//湖南
// if(indexPath.row == 0){
// text = @"长沙";
// }else if(indexPath.row == 1){
// text = @"岳阳";
// }
text = [hnCities objectAtIndex:indexPath.row];
}
[color=violet]cell.textLabel.text = text;
//设置cell右边剪头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;[/color]
return cell;
}
#pragma mark - 每组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0){
return @"广东省";
}else{
return @"湖南省";
}
}
#pragma mark
@end
@interface ViewController ()<UITableViewDataSource>{
NSArray *gdCities;
NSArray *hnCities;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[color=red][b]//1.添加tableview[/b][/color]
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
[self.view addSubview:tableView];
//2.初始化数据
gdCities = @[@"广东", @"深圳", @"梅州", @"东莞"];
hnCities = @[@"长沙", @"岳阳", @"邵阳", @"益阳"];
}
#pragma mark - 数据源方法
#pragma mark - 一共有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
[color=green][b]#pragma mark - 一共有多少行[/b][/color]
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){//广东
// return 4;
return gdCities.count;
}else{//湖南
// return 2;
return hnCities.count;
}
}
[color=darkblue][b]#pragma mark - 返回每行显示的内容[/b][/color]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//indexpath 标示唯一的一行
// indexPath.section == 0;
// indexPath.row == 0;
[color=darkred]UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];[/color]
//设置cell显示的文字
NSString *text = nil;
if(indexPath.section == 0){//广东
// if(indexPath.row == 0){
// text = @"广州";
// }else if(indexPath.row == 1){
// text = @"深圳";
// }else if (indexPath.row == 2){
// text = @"梅州";
// }else if (indexPath.row == 3){
// text = @"东莞";
// }
text = [gdCities objectAtIndex:indexPath.row];
}else{//湖南
// if(indexPath.row == 0){
// text = @"长沙";
// }else if(indexPath.row == 1){
// text = @"岳阳";
// }
text = [hnCities objectAtIndex:indexPath.row];
}
[color=violet]cell.textLabel.text = text;
//设置cell右边剪头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;[/color]
return cell;
}
#pragma mark - 每组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0){
return @"广东省";
}else{
return @"湖南省";
}
}
#pragma mark
@end