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 

转载于:https://www.cnblogs.com/lixiang2015/p/4709586.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值