iOS索引,按字母排序

来源:互联网 发布:php上传文件原理 编辑:程序博客网 时间:2024/05/21 06:50

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
原创粉丝点击