ios 输入数组,生成索引

输入英文数组,建立字典,key是索引数组,value是索引对应的数组。需要提前对数组进行排序,方便后续的分组。取出key数组作为index,
取出vale作为section,生成索引。
//
//  ViewController.m
//  Indexes
//
//  Created by mac on 2017/8/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UITableView* tableView;
@property(nonatomic,strong) NSArray* photoes;
@property(nonatomic,strong) NSMutableArray* arrays;
@property(nonatomic,strong) NSArray* indexs;
@property(nonatomic,assign) UIEdgeInsets insets;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _insets = UIEdgeInsetsMake(0, 15, 0, 0);
    
    NSArray* aa= [[NSArray alloc] init];
    aa = @[@"ab",@"ac",@"fg",@"cd",@"ce",@"dd",@"fb",@"daaa",@"zee",@"bvd",@"c22",@"h",@"dd",@"fb",@"daaa",@"zee",@"bvd"];
    [self change:aa];

//    for(char c = 'A'; c <=  'F'; c++ )
//    {
//        [_indexs addObject:[NSString stringWithFormat:@"%c",c]];
//    }
//    _arrays = @[
//                @[@"ABC",@"aef",@"aLM",@"aPQ"],
//                @[@"bBC",@"bef",@"bLM",@"bPQ"],
//                @[@"cBC",@"cef",@"cLM",@"cPQ"],
//                @[@"DBC",@"Def",@"DLM",@"DPQ"],
//                @[@"EBC",@"Eef",@"ELM",@"EPQ"],
//                @[@"FBC",@"Fef",@"FLM",@"FPQ"],
//                @[@"GBC",@"Gef",@"GLM",@"GPQ"],];
    
    _photoes =@[@"ic_home_gas",@"ic_home_penalty",@"ic_home_elect",@"ic_home_wallet"];
    
    [self initMyTableView];
}
//数组进行处理
-(NSArray*)change:(NSArray*)aa{
    NSArray* test = [[NSArray alloc]init];
    _arrays = [[NSMutableArray alloc] init];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    test = [aa sortedArrayUsingSelector:@selector(compare:)];
    for(NSInteger i =0;i<test.count;i++){
        NSString* first = [[test[i] uppercaseString] substringToIndex:1];
        NSMutableArray* array = [dic objectForKey:first];
        if(array == nil){
            array = [NSMutableArray array];
            [dic setObject:array forKey:first];
            [array addObject:test[i]];
        }else{
            [array addObject:test[i]];
        }
    }
    _indexs = [[dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for(NSInteger a=0;a<_indexs.count;a++){
        [_arrays addObject:[dic objectForKey:_indexs[a]]];
    }
    return _arrays;
}

//初始化UITableView
- (void)initMyTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    //_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.sectionIndexColor = [UIColor blueColor];
    _tableView.sectionIndexTrackingBackgroundColor = [UIColor clearColor];
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //_tableView.separatorStyle = UITableViewCellSelectionStyleNone;;
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    
    [self.view addSubview:_tableView];
}

//返回section中的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[_arrays objectAtIndex:section ] count];
    return [_photoes count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *Identify = @"CELL";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 2, self.view.frame.size.width-15, 2)];
//    line.backgroundColor = [UIColor greenColor];
//    [cell.contentView addSubview:line];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
    }
    //cell.textLabel.text =  [_dictionarys objectAtIndex:indexPath.section * 2 + indexPath.row];
    cell.textLabel.text = _arrays[indexPath.section][indexPath.row];
    UIImage *image = [UIImage imageNamed:_photoes[indexPath.row]];
    CGSize itemSize = CGSizeMake(40, 40);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0.0);
    CGRect imageRect = CGRectMake(-6.0, 0.0, itemSize.width, itemSize.height);
    [image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return cell;
}

//返回索引数组
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return _indexs;
}

//响应点击索引时的委托方法
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    NSInteger count = 0;
    
    for (NSString *character in _indexs) {
        
        if ([[character uppercaseString] hasPrefix:title]) {
            return count;
        }
        
        count++;
    }
    
    return  0;
}

//返回每个索引的内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [_indexs objectAtIndex:section];
}

//返回section的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_indexs count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:_insets];
    }
    if([cell respondsToSelector:@selector(setLayoutMargins:)]){
        [cell setLayoutMargins:_insets];
    }
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值