#import "ViewController.h"
@interface ViewController ()
@property (strong,nonatomic) NSArray *bj;
@property (strong, nonatomic)NSArray *gd;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.bj = @[@"东城", @"西城", @"宣武", @"崇文"];
self.gd = @[@"广东", @"佛山", @"深圳"];
}
#pragma mark -TableView的数据源方法
#pragma mark 分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
#pragma mark 每个分组中显示的表格个数,指定每个分组中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return self.bj.count;
}else{
return self.gd.count;
}
}
#pragma mark 每个表格中显示的数据内容,这里面的内容是由数据源负责的。
//@interface NSIndexPath (UITableView)
//
//+ (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;
//
//@property(nonatomic,readonly) NSInteger section;
//@property(nonatomic,readonly) NSInteger row;
//
//@end
//可以通过当前的indexPath判断出当前的行和组
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString *cityName = nil;
//组
if(indexPath.section == 0) //0组
{
cityName = _bj[indexPath.row];
}else{
cityName = _gd[indexPath.row];
}
[cell.textLabel setText:cityName];
return cell;
}
#pragma mark 分组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return section == 0 ? @"北京" : @"广东";
}
#pragma mark 分组底部的子标题,通常用于描述
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return section == 0 ? @"北京有烤鸭" : @"广东有东莞";
}
@end