Multi group data tableview setting, adding right group index, multi-layer data model setting and valueforkeypath


First look at the effect:
Multi group data tableview setting, adding right group index, multi-layer data model setting and valueforkeypath

The data model here has two layers: each group of cars is a layer model, and each line of car brands in each group is also a layer model.

(1) Let’s create a wscars model first.


#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;


#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;


(2) Then create a car group model, wscargroup.


#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;

stayWSCarGroup.mChinese: (a dictionary conversion model is made here, that is, each car data is converted into wscars object)

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

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

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


(3) And then in theViewController.mDefine the array and translate the dictionary into the model

@property (nonatomic,strong) NSArray *carsArray;  
//Dictionary to dictionary model
- (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];
    return _carsArray;

This completes the array work.

(4) Drag a tableview and define it as a variable. This controller is used as a data source, so it follows the protocol.

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

(5) And set the data source as the current controller, by the way, set a downlink high

- (void)viewDidLoad {
    //Set data source
    //Set row height
    [super viewDidLoad];

(6) Set the group, row and cell data and group name of tableview.

//How many groups are set
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carsArray.count;
//How many rows are set
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WSCarGroup *carGroup=self.carsArray[section];
//Set cell content
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //Cache pool first, performance optimization
    static NSString *[email protected]"car";
    UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    //Take out the data
    WSCarGroup *carGroup=self.carsArray[indexPath.section];
    WSCars *[indexPath.row];
    //Assign to cell;
    cell.imageView.image=[UIImage imageNamed:cars.icon];
    return cell;
//Set group name
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    WSCarGroup *carGroup=self.carsArray[section];
    return carGroup.title;

(7) Set group index

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    //An array needs to be returned
    //Using valueforkey can only be found in this level dictionary, while self. Carsarray is an array without title keyword
    //Valueforkeypath can be used to find the current level and lower level dictionary arrays. There is a path
    return [self.carsArray valueForKeyPath:@"title"];

(8) For display and viewing convenience:

//Hide status bar
    return YES;


——The difficulty lies in the translation of the dictionary into the model, because the model has two levels.
——Add a knowledge point, that is, display group index. usesectionIndexTitlesForTableViewMethod, the return value is an array, so we also use it herevalueForKeyPathThis method takes a string group.

Go to:A small inn — a big river and lake