IOS开发之——列表索引(25)

一概述

本文介绍从plist配置文件中读取数据,并设置到UITableView中;在右侧设置列表索引,通过索引字母,快速定位到所在位置。

<!--more-->

二 效果图

 

三 代码

3.1 OC模式下

Car.h

#import <Foundation/Foundation.h>
​
@interface Car : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
​
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carWithDict:(NSDictionary *)dict;
​
//传入一个包含字典的数组,返回一个Car模型的数组
+(NSArray *)carsWithArray:(NSArray *)array;
​
@end

Car.m

#import "Car.h"
@implementation Car
- (instancetype)initWithDict:(NSDictionary *)dict
{
    self=[super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+ (instancetype)carWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
+ (NSArray *)carsWithArray:(NSArray *)array
{
    NSMutableArray *arrayM=[NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self carWithDict:dict]];
    }
    return arrayM;
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@:%p>{name:%@,icon:%@}",self.class,self,self.name,self.icon];
}
@end

CarGroup.h

#import <Foundation/Foundation.h>
@interface CarGroup : NSObject
//首字母
@property (nonatomic,copy) NSString *title;
//车的数组
@property (nonatomic,strong) NSArray *cars;
​
- (instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)carGroupWithDict:(NSDictionary *)dict;
+(NSArray *)carGroups;
@end

CarGroup.m

#import "CarGroup.h"
#import "Car.h"
​
@implementation CarGroup
- (instancetype)initWithDict:(NSDictionary *)dict
{
    self=[self init];
    if (self) {
        //[self setValuesForKeysWithDictionary:dict];
        [self setValue:dict[@"title"] forKey:@"title"];
        self.cars=[Car carsWithArray:dict[@"cars"]];
    }
    return self;
}
+ (instancetype)carGroupWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
+(NSArray *)carGroups
{
    NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil]];
    NSMutableArray *arrayM=[NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self carGroupWithDict:dict]];
    }
    return arrayM;
}
- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: %p> {title: %@, cars: %@}", self.class, self, self.title, self.cars];
}
@end

ViewController.m

#import "ViewController.h"
#import "Car.h"
#import "CarGroup.h"
​
@interface ViewController ()<UITableViewDataSource>
@property (nonatomic,strong) NSArray *carGroups;
@property (nonatomic,strong) UITableView *tableView;
@end
​
@implementation ViewController
​
- (UITableView *)tableView
{
    if (_tableView==nil) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (NSArray *)carGroups
{
    if (_carGroups==nil) {
        _carGroups=[CarGroup carGroups];
    }
    return _carGroups;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
    // Do any additional setup after loading the view.
}
#pragma -数据源方法
//分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.carGroups.count;
}
//每一组的总数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CarGroup *group=self.carGroups[section];
    return group.cars.count;
}
//每一单元内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //可重用标识符
    static NSString *ID=@"Cell";
    //让表格缓冲区查找可重用cell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    //如果没有找到可重用cell
    if (cell==nil) {
    //实例化cell
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    //设置cell内容
    CarGroup *group=self.carGroups[indexPath.section];
    Car *car=group.cars[indexPath.row];
    //设置数据
    cell.imageView.image=[UIImage imageNamed:car.icon];
    cell.textLabel.text=car.name;
    return cell;
}
//设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CarGroup *group=self.carGroups[section];
    return group.title;
}
//右侧索引列表
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.carGroups valueForKeyPath:@"title"];
}
@en
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值