图片描述

说明: title 属性表示该 item 下汽车名字的首字母, cars 属性存放首字母为 title 的汽车, icon 属性存放图片的名称, name 属性存放汽车的名字。

二、代码实例

  新建一个继承自 NSObject 的类,命名为 WJQCars ,该类用于存放首字母相同的汽车。

//  WJQCars.h
@interface WJQCars : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
 
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carsWithDict:(NSDictionary *)dict;
@end
 
//  WJQCars.m
@implementation WJQCars
  
- (instancetype)initWithDict:(NSDictionary *)dict {
     self = [super init];
      if (self) {
          self.name = dict[@"name"];
          self.icon = dict[@"icon"];
      }
     return self;
 }
 
 + (instancetype)carsWithDict:(NSDictionary *)dict {
     return [[self alloc] initWithDict:dict];
 }

新建一个继承自 NSObject 的类,并命名为 WJQCarsGroup ,该类用于存放不同的 WJQCars

//  WJQCarsGroup.h
 @interface WJQCarsGroup : NSObject
 @property (nonatomic, copy) NSString *title;
 @property (nonatomic, strong) NSArray *cars;
 
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)carsGroupWithDict:(NSDictionary *)dict;
@end

WJQCarsGroup.m 中导入 WJQCars.h ,实现文件代码如下:

    //  WJQCarsGroup.m
    - (instancetype)initWithDict:(NSDictionary *)dict {
        self = [super init];
        if (self) {
            self.title = dict[@"title"];
            NSArray *dictCars = dict[@"cars"];
            // 有助于提高性能
            NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictCars.count];
            for (NSDictionary *dict in dictCars) {
                WJQCars *wcars = [[WJQCars alloc] initWithDict:dict];
                [arrayM addObject:wcars];
            }
            self.cars = arrayM;
        }
        return self;
    }
    
    + (instancetype)carsGroupWithDict:(NSDictionary *)dict {
        return [[self alloc]initWithDict:dict];
    }

ViewController.m 中导入 WJQCarsGroup.h、WJQCars.h




 //  ViewController.m
     @interface ViewController () <UITableViewDataSource>
     @property (nonatomic, strong) IBOutlet UITableView *tableView;
     @property (nonatomic, strong) NSArray *car;
     @end

将 tableView 属性与 Main.storyboard 中拖入的 Table View 视图建立连接。并让 ViewController 遵守 UITableViewDataSource 协议。

//  ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 60;
    self.tableView.dataSource = self;
    NSLog(@"self.car.count = %d", self.car.count);
}

// 从包中读取数据,实现字典转模型
- (NSArray *)car {
    if (_car == nil) {
        NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
        NSArray *arrayM = [NSArray arrayWithContentsOfFile:fullPath];
        NSMutableArray *carsArray = [NSMutableArray array];
        for (NSDictionary *dict in arrayM) {
            WJQCarsGroup *carsGroup = [WJQCarsGroup carsGroupWithDict:dict];
            [carsArray addObject:carsGroup];
        }
        _car = [carsArray copy];
    }
    return _car;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.car.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    WJQCarsGroup *carsGroup = self.car[section];
    return carsGroup.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"car";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    WJQCarsGroup *carsGroup = self.car[indexPath.section];
    WJQCars *cars = carsGroup.cars[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:cars.icon];
    cell.textLabel.text = cars.name;
    return cell;
}

// 设置每组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    WJQCarsGroup *carsGroup = self.car[section];
    return carsGroup.title;
}

// 设置首字母索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSArray *title = [self.car valueForKeyPath:@"title"];  // 使用KVC机制
    return title;
}

xiaomayi2012
300 声望7 粉丝