ios-day06-03(UITableView的索引条、一个对象作为另一个对象的属性、valueForKey和valueForKeyPath的区别)

源码下载地址:http://download.csdn.net/detail/liu537192/8461813


效果图:

         


核心代码:

//
//  LiuJieViewController.m
//  03-汽车品牌
//
//  Created by Mac on 15-2-24.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieViewController.h"
#import "LiuJieCarGroup.h"
#import "LiuJieCar.h"

@interface LiuJieViewController () 
    
    
     
     
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
 *  车品牌组数据
 */
@property (nonatomic,strong) NSArray *groups;
@end

@implementation LiuJieViewController

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

- (NSArray *)groups{

    if (_groups == nil) {
        // 获取plist文件的全路径
        NSString *path = [[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil];
        
        // 加载数据
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            LiuJieCarGroup *group = [LiuJieCarGroup groupWithDict:dict];
            
            [tempArray addObject:group];
        }
        
        _groups = tempArray;
    }
    
    return _groups;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    
    return self.groups.count;
}

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

    LiuJieCarGroup *group = self.groups[section];
    
    return group.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 定义一个循环标识
    static NSString *ID = @"car";
    
    // 从缓存池中取出可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 缓存池中没有可循环利用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }
    // 设置数据
    LiuJieCarGroup *group = self.groups[indexPath.section];
    LiuJieCar *car = group.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{

    LiuJieCarGroup *group = self.groups[section];
    return group.title;
}
/**
 *  返回右边索引条显示的字符串数据
 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    // valueForKey:只能取直接属性,不能取间接属性。
    // [对象 valueForKey:@"title"]:获取对象的title属性的值
    // [数组 valueForKey:@"title"]:无法获取数组中每个对象的title属性的值
    
    // valueForKeyPath:既能取直接属性,又能取间接属性
    // [对象 valueForKeyPath:@"title"]:获取对象的title属性的值
    // [数组 valueForKeyPath:@"title"]:获取数组中每个对象的title属性的值
    return [self.groups valueForKeyPath:@"title"];
}

// 隐藏状态栏
- (BOOL)prefersStatusBarHidden{
    
    return YES;
}

@end
//
//  LiuJieCarGroup.h
//  03-汽车品牌
//
//  Created by Mac on 15-2-27.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      

@interface LiuJieCarGroup : NSObject
/**
 *  每组的标题
 */
@property (nonatomic,copy) NSString *title;
/**
 *  存放所有的汽车品牌(里面装的都是LiuJieCar的模型)
 */
@property (nonatomic,strong) NSArray *cars;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)groupWithDict:(NSDictionary *)dict;
@end
//
//  LiuJieCarGroup.m
//  03-汽车品牌
//
//  Created by Mac on 15-2-27.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieCarGroup.h"
#import "LiuJieCar.h"

@implementation LiuJieCarGroup

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        // 赋值标题
        self.title = dict[@"title"];
        
        // 
        NSArray *dictArray = dict[@"cars"];
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            LiuJieCar *car = [LiuJieCar carWithDict:dict];
            [tempArray addObject:car];
        }
        self.cars = tempArray;
    }
    
    return self;
}

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

    return [[self alloc] initWithDict:dict];
}

@end
//
//  LiuJieCar.h
//  03-汽车品牌
//
//  Created by Mac on 15-2-27.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
      
      
       
       

@interface LiuJieCar : NSObject

@property (nonatomic,copy) NSString *icon;

@property (nonatomic,copy) NSString *name;

+ (instancetype)carWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

@end
//
//  LiuJieCar.m
//  03-汽车品牌
//
//  Created by Mac on 15-2-27.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieCar.h"

@implementation LiuJieCar

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

    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

@end

      
      
     
     
    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值