tableview 侧边 index

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *userArray; //数据源数组
@property (strong,nonatomic) NSDictionary *dicArr;
@property (nonatomic, strong) UITableView *tableView;
//UITableView索引搜索工具
@property (nonatomic,strong) NSMutableArray *keys;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  configureSections];
    CGRect frame = self.view.bounds;
    self.tableView =
    [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (NSString *)firstCharactor:(NSString *)aString
{
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:aString];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
    //转化为大写拼音
    NSString *pinYin = [str capitalizedString];
    //获取并返回首字母
    return [pinYin substringToIndex:1];
}


//配置分组信息
- (void)configureSections {
    //初始化测试数据
    self.dicArr = [[NSMutableDictionary alloc] init];
    _userArray = [NSMutableArray arrayWithObjects:@"一小",@"二小",@"三小",@"四小",@"五小",@"六小",@"二中",@"三中",@"四中",@"八中",@"一中",@"四高",@"五高",@"一高", nil];


    for (NSString *name in _userArray) {
        NSString * A= [self firstCharactor:name];
        NSMutableArray *mArr=[self.dicArr objectForKey:A];
        if (!mArr) {
            mArr = [[NSMutableArray alloc]init];
        }
        [mArr addObject:name];
        [self.dicArr setValue:mArr forKey:A];
    }
    NSArray *keysArray = [self.dicArr allKeys];

    NSArray *resultArray = [keysArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

        return [obj1 compare:obj2 options:NSNumericSearch];

    }];
    self.keys= [[NSMutableArray alloc]initWithArray: resultArray];
}

#pragma mark -- delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [self.keys count];

}

//设置每个Section下面的cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // The number of time zones in the section is the count of the array associated with the section in the sections array.
     return [[self.dicArr objectForKey:[self.keys objectAtIndex:section]] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
     cell.textLabel.text = [[self.dicArr objectForKey:[self.keys objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

     NSLog(@"%@", [[self.dicArr objectForKey:[self.keys objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]);

}

/*
 * 跟section有关的设定
 */
//设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      return [self.keys objectAtIndex:section];
}

//设置索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.keys;
}

//索引点击事件
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    return index;
}
@end  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值