34.小项目:汽车列表 版本2.0

更新说明:增加多组数据展示 

------------- ViewController.m -------------

#import "ViewController.h"

#import "CZCarGroup.h"


@interface ViewController () <UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *carGroups;

@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.tableView.dataSource = self;

}


- (NSArray *)carGroups

{

    if (_carGroups == nil)

    {

        CZCarGroup *carG0 = [[CZCarGroup alloc] init];

        carG0.header = @"德系品牌";

        carG0.footer = @"高端大气上档次,世界一流品牌";

        carG0.cars = @[@"宝马", @"奔驰"];

        

        CZCarGroup *carG1 = [[CZCarGroup alloc] init];

        carG1.header = @"日系品牌";

        carG1.footer = @"燃油经济,性价比高";

        carG1.cars = @[@"本田", @"丰田"];

        

        CZCarGroup *carG2 = [[CZCarGroup alloc] init];

        carG2.header = @"国产品牌";

        carG2.footer = @"实用性高,价格实惠,支持国产";

        carG2.cars = @[@"奇瑞"];

        _carGroups = @[carG0, carG1, carG2];

    }

    return _carGroups;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.carGroups.count;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [self.carGroups[section] cars].count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    CZCarGroup * carG = self.carGroups[indexPath.section];

    cell.textLabel.text = carG.cars[indexPath.row];

    return cell;

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return [self.carGroups[section] header];

}


- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    return [self.carGroups[section] footer];

}

@end 

------------- CZCarGroup.h -------------

#import <Foundation/Foundation.h>


@interface CZCarGroup : NSObject

@property (nonatomic,copy) NSString *header;

@property (nonatomic,copy) NSString *footer;

@property (nonatomic,strong) NSArray *cars;

@end 

------------- CZCarGroup.m ------------- 

#import "CZCarGroup.h"


@implementation CZCarGroup


@end 

posted @ 2015-08-07 00:49  我要选李白  阅读(111)  评论(0编辑  收藏  举报