UITableView之分组案例

首先我们先看下我们要做到那种效果源码会在底部给出链接

这里写图片描述

其次我们分下下数据
这里写图片描述

我们分析下这张图:首先数据的根是个数组里面是每个字典并包含header,footer,和cars属性;其次cars也是个数组里面也是每个字典并右包含name,icon属性。OK根据面向对象思想的思想我们可以把根视图看成个对象把而cars看成根视图每个字典里的cars对象。这也就是IOS中的数据转模型,这样的好处就是我们将数据封装好就可以 针对性的处理数据。可能对文字有迷惑下面我们看具体将文字转化为代码做法:

之前我们说了根视图作为一个对象so我们首先创建一个根视图对象

GGroupCarModle.h

//
//  GGroupCarModle.h
//  TableView之二分组(汽车案例)
//
//  Created by gaocai on 16/7/25.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface GGroupCarModle : NSObject

@property (nonatomic, copy) NSString *header;
@property (nonatomic, copy) NSString *footer;
//将汽车封装成一个汽车模型
@property (nonatomic, strong) NSArray *carArray;

+ (instancetype)groupCarWithDictionary:(NSDictionary *)dictionary;


@end

GGroupCarModle.m

//
//  GGroupCarModle.m
//  TableView之二分组(汽车案例)
//
//  Created by gaocai on 16/7/25.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import "GGroupCarModle.h"
#import "GCarModel.h"

@implementation GGroupCarModle

+ (instancetype)groupCarWithDictionary:(NSDictionary *)dict {

    GGroupCarModle *groupCar = [[GGroupCarModle alloc] init];

    groupCar.header = dict[@"header"];
    groupCar.footer = dict[@"footer"];

    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dictionary in dict[@"cars"]) {//每个汽车都封装成一个模型
        GCarModel *carModel = [GCarModel carWithDictionary:dictionary];
        [array addObject:carModel];
    }
    //将模型放到数组当中
    groupCar.carArray = array;
    return groupCar;

}
@end

说明:可以对照着图看代码我们定义三个属性header,footer,cars;以上就是我们的根视图模型我们只处理根视图下的数据而cars对象的数据则交于cars对象去处理

其次创建我们的cars对象模型

GCarModel.h

//
//  GCarModel.h
//  TableView之二分组(汽车案例)
//
//  Created by gaocai on 16/7/25.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface GCarModel : NSObject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *icon;

+ (instancetype)carWithDictionary:(NSDictionary *)dictionary;
@end

GCarModel.m

//
//  GCarModel.m
//  TableView之二分组(汽车案例)
//
//  Created by gaocai on 16/7/25.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import "GCarModel.h"

@implementation GCarModel


+ (instancetype)carWithDictionary:(NSDictionary *)dictionary {

    //存储每个汽车模型的名字和图标
    GCarModel *carModel = [[GCarModel alloc] init];
    carModel.name = dictionary[@"name"];
    carModel.icon = dictionary[@"icon"];

    return carModel;
}

@end
以上就是我们对数据转模型的处理最后就是我们对模型的使用了
//
//  ViewController.m
//  TableView之二分组(汽车案例)
//
//  Created by gaocai on 16/7/25.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import "ViewController.h"
#import "GGroupCarModle.h"
#import "GCarModel.h"

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *groupCarArray;

@end

@implementation ViewController

//懒加载数据
- (NSArray *)groupCarArray {

    if (_groupCarArray == nil) {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil]];

        NSMutableArray *temp = [NSMutableArray array];
        for (NSDictionary *dictionary in array) {
            [temp addObject:[GGroupCarModle groupCarWithDictionary:dictionary]];
        }
        _groupCarArray = temp;
        NSLog(@"%@", _groupCarArray);
    }

    return _groupCarArray;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    //设置
    _tableView.dataSource = self;
}


#pragma mark - UITableViewDataSource代理方法

//一共有多少行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupCarArray.count;

}

//每一行有多少数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    GGroupCarModle *groupModel = self.groupCarArray[section];
    return groupModel.carArray.count;
}

//每一行显示的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //每一组的模型数据
    GGroupCarModle *groupModel = self.groupCarArray[indexPath.section];
    //每一组每一行的模型数组(汽车模型)
    GCarModel *carModel = groupModel.carArray[indexPath.row];

    //设置数据
    cell.textLabel.text = carModel.name;
    cell.imageView.image = [UIImage imageNamed:carModel.icon];
    NSLog(@"%@", carModel.icon);

    return cell;

}

//头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    //获得哪一组
    GGroupCarModle *groupModle = self.groupCarArray[section];
    //返回哪一组的头部
    return groupModle.header;
}

//尾部
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    GGroupCarModle *groupModle = self.groupCarArray[section];
    return groupModle.footer;

}

@end

DEMO

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值