多組數據的tableView設置、增長右側組索引、多層數據模型設置以及valueForKeyPath

先看效果:
圖片描述數組

這裏的數據模型有兩層:每一組汽車是一層模型,每一組裏面的每一行汽車品牌也是一層模型。緩存

(1)咱們先建立一個WSCars模型。

WSCars.h中:性能優化

#import <Foundation/Foundation.h>

@interface WSCars : NSObject
@property(nonatomic,copy) NSString *icon;
@property(nonatomic,copy) NSString *name;

+(WSCars *)carsWithDict:(NSDictionary *)dict;
-(WSCars *)initWithDict:(NSDictionary *)dict;
@end

WSCars.m中:性能

#import "WSCars.h"

@implementation WSCars


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

-(WSCars *)initWithDict:(NSDictionary *)dict{
    if ([super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

@end

(2)再建立一個汽車組模型,WSCarGroup。

WSCarGroup.h中:優化

#import <Foundation/Foundation.h>

@interface WSCarGroup : NSObject
@property(nonatomic,copy) NSString * title;
@property(nonatomic,strong) NSArray *cars;

+(WSCarGroup *)carGroupWithDict:(NSDictionary *)dict;
-(WSCarGroup *)initWithDict:(NSDictionary *)dict;
@end

WSCarGroup.m中:(此處作了1次字典轉模型,即把每一個汽車數據轉成WSCars對象)ui

#import "WSCarGroup.h"
#import "WSCars.h"
@implementation WSCarGroup

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

-(WSCarGroup *)initWithDict:(NSDictionary *)dict{
    if ([super init]) {
        self.title=dict[@"title"];
        
        NSArray *dictArray=dict[@"cars"];
        NSMutableArray *muArray=[[NSMutableArray alloc]init];
        for (NSDictionary * dic in dictArray) {
            WSCars *car=[[WSCars alloc]initWithDict:dic];
            [muArray addObject:car];
        }
        self.cars=muArray;
    }
    return self;
}

@end

(3)而後在ViewController.m中,定義數組,而且把字典轉模型

@property (nonatomic,strong) NSArray *carsArray;
//字典轉模型
- (NSArray *)carsArray{
    if (_carsArray==nil) {
        NSString *path=[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil];
        NSArray *totalArray=[NSArray arrayWithContentsOfFile:path];
        NSMutableArray *muArray=[[NSMutableArray alloc]init];
        for (NSDictionary *dict in totalArray) {
            WSCarGroup *carGroup=[[WSCarGroup alloc]initWithDict:dict];
            [muArray addObject:carGroup];
        }
        _carsArray=muArray;
    }
    return _carsArray;
}

數組工做至此完成。atom

(4)拖拽一個tableView,而且定義成變量。這個控制器被當成數據源,因此遵照協議。

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

(5)而且把數據源設置成當前控制器,順便設置一下行高

- (void)viewDidLoad {
    //設置數據源
    self.tableView.dataSource=self;
    //設置行高
    self.tableView.rowHeight=60;
    [super viewDidLoad];
}

(6)設置tableView的組、行和cell數據和組名字。

//設置多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carsArray.count;
}
//設置多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WSCarGroup *carGroup=self.carsArray[section];
    return carGroup.cars.count;
}
//設置cell內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //先緩存池,性能優化
    static NSString *ID=@"car";
    UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //取出數據
    WSCarGroup *carGroup=self.carsArray[indexPath.section];
    WSCars *cars=carGroup.cars[indexPath.row];
    //賦值給cell
    cell.textLabel.text=cars.name;
    cell.imageView.image=[UIImage imageNamed:cars.icon];
    return cell;
}
//設置組名
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    WSCarGroup *carGroup=self.carsArray[section];
    return carGroup.title;
}

(7)設置組索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    //須要返回一個數組
    //用valueForKey只能在本層級字典中查找,而self.carsArray是數組,且沒有title關鍵字
    //用valueForKeyPath能夠在本級及下級字典數組中查找,有path路徑
    return [self.carsArray valueForKeyPath:@"title"];
}

(8)爲方便展現觀看:

//隱藏狀態欄
-(BOOL)prefersStatusBarHidden{
    return YES;
}

總結:spa

——難度在於字典轉模型的地方,由於模型有2層級。
——增長了一個知識點,即顯示組索引。用sectionIndexTitlesForTableView方法,返回值是一個數組,因此咱們這裏也用到了valueForKeyPath這個方法取得一個字符串組。.net

轉至:小小的客棧——大大的江湖code

相關文章
相關標籤/搜索