#pragma mark - 数据源方法
/**
* 一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
/**
* 第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) { // 德系品牌
return 3;
} else if (section == 1) { // 日韩品牌
return 4;
} else { // 欧系品牌
return 2;
}
}
/**
* 每一行显示怎样的内容(cell)
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (indexPath.section == 0) { // 德系品牌(第0组)
if (indexPath.row == 0) { // 第0组的第0行
cell.textLabel.text = @"奥迪";
} else if (indexPath.row == 1) { // 第0组的第1行
cell.textLabel.text = @"宝马";
} else if (indexPath.row == 2) {
cell.textLabel.text = @"奔驰";
}
} else if (indexPath.section == 1) { // 日韩品牌(第1组)
if (indexPath.row == 0) { // 第1组的第0行
cell.textLabel.text = @"本田";
} else if (indexPath.row == 1) { // 第1组的第1行
cell.textLabel.text = @"丰田";
} else if (indexPath.row == 2) {
cell.textLabel.text = @"铃木";
} else if (indexPath.row == 3) {
cell.textLabel.text = @"三菱";
}
} else if (indexPath.section == 2) { // 欧系品牌(第2组)
if (indexPath.row == 0) { // 第2组的第0行
cell.textLabel.text = @"兰博基尼";
} else if (indexPath.row == 1) { // 第2组的第1行
cell.textLabel.text = @"劳斯莱斯";
}
}
return cell;
}
/**
* 第section组显示怎样的头部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"德系品牌";
} else if (section == 1) {
return @"日韩品牌";
} else {
return @"欧系品牌";
}
}
/**
* 第section组显示怎样的尾部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
if (section == 0) {
return @"世界一流品牌";
} else if(section == 1) {
return @"牛逼哄哄";
} else {
return @"价格昂贵";
}
}
11111111111111111111111111111111111111111111111111111111111111111111111111111111
#import <Foundation/Foundation.h>
@interface MJCarGroup : NSObject
/**
* 头部标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 尾部标题(描述)
*/
@property (nonatomic, copy) NSString *desc;
/**
* 这组的所有车(字符串)
*/
@property (nonatomic, strong) NSArray *cars;
@end
@interface MJViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 设置数据源
self.tableView.dataSource = self;
}
// 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (NSArray *)carGroups
{
if (_carGroups == nil) {
// 初始化
// 德系品牌
MJCarGroup *cg1 = [[MJCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"德系品牌好";
cg1.cars = @[@"奥迪", @"宝马"];
// 日韩品牌
MJCarGroup *cg2 = [[MJCarGroup alloc] init];
cg2.title = @"日韩品牌";
cg2.desc = @"日韩品牌很好";
cg2.cars = @[@"本田", @"丰田"];
// 欧系品牌
MJCarGroup *cg3 = [[MJCarGroup alloc] init];
cg3.title = @"欧系品牌";
cg3.desc = @"欧系品牌非常好";
cg3.cars = @[@"兰博基尼", @"法拉利"];
_carGroups = @[cg1, cg2, cg3];
}
return _carGroups;
}
#pragma mark - 数据源方法
/**
* 一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carGroups.count;
}
/**
* 第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取得第section组对应的模型
MJCarGroup *cg = self.carGroups[section];
return cg.cars.count;
}
/**
* 每一行显示怎样的内容(cell)
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// 取出第indexPath.section组对应的模型
MJCarGroup *cg = self.carGroups[indexPath.section];
// 取车第indexPath.row这行对应的品牌名称
NSString *car = cg.cars[indexPath.row];
// 设置cell显示的文字
cell.textLabel.text = car;
return cell;
}
/**
* 第section组显示怎样的头部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
MJCarGroup *cg = self.carGroups[section];
return cg.title;
}
/**
* 第section组显示怎样的尾部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
MJCarGroup *cg = self.carGroups[section];
return cg.desc;
}
@end