UITableView

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值