iOS索引,按字母排序

table的索引,汽车之家之类那种选车索引。
最主要的就是 sectionIndexTitlesForTableView:(UITableView *)tableView 这个方法;
上代码了
我自己用了一个plist 类似于这种
我附上一个demo,自己看看吧 github上
demo链接
https://github.com/KingOfZhang/SuoYin/tree/master
{
A = (
A1,
A2,
A3,
A4
);
B = (
B1,
B2,
B3,
B4
);
C = (
C1,
C2,
C3,
C4,
C5,
C6
);

}

//
//  ViewController.m
//  UISearBar
//
//  Created by 易云时代 on 2017/8/21.
//  Copyright © 2017年 XW. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *arr;
    NSDictionary *_dataSource;
}
@property (nonatomic, strong) UITableView *table;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WITH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    arr = [[NSMutableArray alloc]init];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"NameList" ofType:@"plist"];
    _dataSource = [NSDictionary dictionaryWithContentsOfFile:path];

    NSArray *atttt = [_dataSource allKeys];
    atttt = [atttt sortedArrayUsingSelector:@selector(compare:)];
    arr = [atttt copy];
    [_table reloadData];
}

#pragma mark TableViewDelegateAndDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 20;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return arr[section];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pp"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"pp"];
    }
    NSArray *st = [_dataSource objectForKey:arr[indexPath.section]];;
    cell.textLabel.text = st[indexPath.row];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [_dataSource objectForKey:arr[section]];
    return array.count;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return arr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值