01-UI基础-04-02-UITableView多组数据显示

##先看效果 ##具体实现 ###1、准备资料 图片资源和plist文件(源码中包含) ###2、建立模型

  • Car模型
//JCar.h文件
@interface JCar : NSObject
/**
 *  图标
 */
@property (nonatomic,copy) NSString *icon;
/**
 *  名称
 */
@property (nonatomic,copy) NSString *name;
+ (instancetype)carWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end

//JCar.m文件
@implementation JCar
+ (instancetype)carWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end


  • Group模型
// JCarGroup.h文件
@interface JCarGroup : NSObject
/**
 *  组标题
 */
@property (nonatomic,copy) NSString *title;
/**
 *  该组下所有的Car
 */
@property (nonatomic,strong) NSArray *cars;
+ (instancetype)groupWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end

// JCarGroup.m文件
#import "JCarGroup.h"
#import "JCar.h"

@implementation JCarGroup

+ (instancetype)groupWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        // 赋值组名称
        self.title = dict[@"title"];
        
        // 组中的汽车列表
        NSArray *dictArray = dict[@"cars"];
        NSMutableArray *carArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            JCar *car = [JCar carWithDict:dict];
            [carArray addObject:car];
        }
        self.cars = carArray;
    }
    return self;
}
@end

###3、实现效果

//
//  ViewController.m
//  01-汽车品牌
//
//  Created by yshye on 15/12/15.
//  Copyright © 2015年 yshye. All rights reserved.
//

#import "ViewController.h"
#import "JCar.h"
#import "JCarGroup.h"

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

/**
 *  车品牌数组
 */
@property (nonatomic,strong) NSArray *carGroups;

@end

@implementation ViewController

- (NSArray *)carGroups{
    if (_carGroups == nil) {
        // 初始化
        // 1.获得plist全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        // 3.将dictArray里面的所有字典转成模型对象,放在新的数组中
        NSMutableArray *groupCar = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1 创建模型对象
            JCarGroup *group = [JCarGroup groupWithDict:dict];
            // 3.2 添加模型对象到数组中
            [groupCar addObject:group];
        }
        // 4.赋值
        _carGroups = groupCar;
    }
    return _carGroups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
}

#pragma mark - 数据源方法
/**
 *  一共有多少数据,不写时,默认是一组
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
}

/**
 *  第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    JCarGroup *cg = self.carGroups[section];
    NSArray * cars =cg.cars;
    return cars.count;
}

/**
 *  每一行显示怎么样的内容(call)
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 1.定义一个循环标示
    static NSString *ID = @"car";
    // 2.从缓存池中取出可循环利用的cell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    // 3.缓存池中没有可循环利用的call
    if (cell == nil) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 4.设置数据
    
    JCarGroup *cg = self.carGroups[indexPath.section];
    JCar *car =cg.cars[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:car.icon];
    
    cell.textLabel.text =car.name;
    return cell;
}

/**
 *  设置第section组显示的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    JCarGroup *cg = self.carGroups[section];
    return cg.title;
}
/**
  *  设置第section组显示的尾部标题
  */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    JCarGroup *cg = self.carGroups[section];
    return @"";
}

/**
 *  设置右侧导航索引
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [self.carGroups valueForKeyPath:@"title"];
}

- (BOOL)prefersStatusBarHidden{
    return YES;
}
#pragma mark - 代理方法
///**
// *  数据显示高度
// */
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return [super ta];
//}
///**
// *  头部数据显示高度
// */
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 30;
//}
//
///**
// *  尾部数据显示高度
// */
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 30;
//}
@end

##源码 01-汽车品牌.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值