#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) {
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars" ofType:@"plist"]];
NSMutableArray *temp = [NSMutableArray array];
for (NSDictionary *carGroupDict in dictArray) {
XMGCarGroup *group = [XMGCarGroup carGroupWithDict:carGroupDict];
[temp addObject:group];
}
_carGroups = temp;
}
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