#import "ViewController.h"
#import "XMGCarGroup.h"
#import "XMGCar.h"
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation ViewController
- (NSArray *)carGroups
{
if (!_carGroups) {
XMGCarGroup *group0 = [[XMGCarGroup alloc] init];
group0.header = @"德系品牌";
group0.footer = @"德系品牌JKJKJKJKJKJK";
group0.cars = @[
[XMGCar carWithName:@"奔驰" icon:@"m_2_100"],
[XMGCar carWithName:@"宝马" icon:@"m_3_100"]
];
XMGCarGroup *group1 = [[XMGCarGroup alloc] init];
group1.header = @"日系品牌";
group1.footer = @"日系品牌JKJKJKJKJKJK";
group1.cars = @[
[XMGCar carWithName:@"丰田" icon:@"m_7_100"],
[XMGCar carWithName:@"马自达" icon:@"m_18_100"],
[XMGCar carWithName:@"本田" icon:@"m_26_100"]
];
XMGCarGroup *group2 = [[XMGCarGroup alloc] init];
group2.header = @"天系品牌";
group2.footer = @"天系品牌JKJKJKJKJKJK";
group2.cars = @[
[XMGCar carWithName:@"比亚迪" icon:@"m_15_100"]
];
_carGroups = @[group0,group1,group2];
}
return _carGroups;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carGroups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
XMGCarGroup *group = self.carGroups[section];
return group.cars.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
XMGCarGroup *group = self.carGroups[indexPath.section];
XMGCar *car = group.cars[indexPath.row];
cell.textLabel.text = car.name;
cell.imageView.image = [UIImage imageNamed:car.icon];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
XMGCarGroup *group = self.carGroups[section];
return group.header;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
XMGCarGroup *group = self.carGroups[section];
return group.footer;
}
@end