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

Time:2021-7-23

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.

stayWSCars.hMedium:

#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

stayWSCars.mMedium:

#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) Then create a car group model, wscargroup.

stayWSCarGroup.hMedium:

#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

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]) {
        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) 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];
        }
        _carsArray=muArray;
    }
    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; 
@end

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

- (void)viewDidLoad {
    //Set data source
    self.tableView.dataSource=self;
    //Set row height
    self.tableView.rowHeight=60;
    [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];
    return carGroup.cars.count;
}
//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 *cars=carGroup.cars[indexPath.row];
    //Assign to cell
    cell.textLabel.text=cars.name;
    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
-(BOOL)prefersStatusBarHidden{
    return YES;
}

Conclusion:

——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