MVC架构

•Mvc的宗旨   只要改了模型view跟着改
•改模型状态就会改 实现代码不用动

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值