带右边索引的 UITableView

//

//  TSFanyiFileSelectLanguageVC.m

//  TranslateSoftware

//

//  Created by boniu on 2022/5/13.

//

#import "TSFanyiFileSelectLanguageVC.h"

#import "TSFanyiFileSelectLanguageCell.h"

@interface TSFanyiFileSelectLanguageVC ()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *closeView;

@property (nonatomic, strong) UITableView *tabView;

@property (nonatomic, strong) NSDictionary *dataDic;

@property (nonatomic, strong) NSArray *keyDatas;

@end

@implementation TSFanyiFileSelectLanguageVC

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self setupUI];

    [self requestList];

}

#pragma mark点击事件

- (void)tapGestureClick {


    [self dismissViewControllerAnimated:YES completion:nil];

}

#pragma markui

- (void)setupUI {

    self.view.backgroundColor = [[UIColor hex:@"#000000"] colorWithAlphaComponent:0.0];

    [self.view addSubview:self.bgView];

    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.left.bottom.equalTo(self.view);

        make.top.equalTo(self.view).offset(44);

        make.bottom.equalTo(self.view).offset(16);;

    }];


    self.tabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

    _tabView.delegate = self;

    _tabView.dataSource = self;

    _tabView.backgroundColor = [UIColor clearColor];

//    _tabView.sectionHeaderHeight = 0;

//    _tabView.sectionFooterHeight = 0;

    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;

    _tabView.showsVerticalScrollIndicator = NO;

    _tabView.showsHorizontalScrollIndicator = NO;

    [_tabView registerClass:[TSFanyiFileSelectLanguageCell class] forCellReuseIdentifier:@"TSFanyiFileSelectLanguageCell"];

    _tabView.sectionIndexColor = [UIColor hex:@"#000000"];//设置默认时索引值颜色

//    _tabView.sectionIndexTrackingBackgroundColor = [UIColor grayColor];//设置选中时,索引背景颜色

//    _tabView.sectionIndexBackgroundColor = [UIColor clearColor];// 设置默认时,索引的背景颜色


    if(@available(iOS11.0, *)) {

        _tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

    }else{

        // Fallback on earlier versions

    }

    [_bgView addSubview:self.tabView];

//    self.tabView.tableHeaderView = self.headView;

    self.tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSizeScale(20) + kTabbarSafeBottomMargin + kSizeScale(60))];

    [_tabView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.left.equalTo(_bgView);

        make.top.equalTo(_bgView.mas_top).offset(0);

        make.bottom.equalTo(_bgView.mas_bottom);

    }];


    [_bgView addSubview:self.closeView];

    [_closeView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.right.top.equalTo(_bgView).offset(0);

        make.height.mas_equalTo(kSizeScale(40));

    }];


    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick)];

    [_closeView addGestureRecognizer:tapGesture];


    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick)];

    [_closeView addGestureRecognizer:panGesture];

}

#pragma mark - tab delegate&&dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    return self.keyDatas.count;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    NSString*key =self.keyDatas[section];

    NSArray*arr =self.dataDic[key];

    return arr.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return kSizeScale(30);

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {


    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kSizeScale(30))];

    bgView.backgroundColor = [UIColor hex:@"#FAFBFD"];

    NSString*key =self.keyDatas[section];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kSizeScale(16), 0, kSizeScale(300), kSizeScale(30))];

    label.text= key;

    label.textColor = [UIColor hex:@"#333333"];

    label.font = kFONT_PF_Normal(14);

    [bgViewaddSubview:label];


    returnbgView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return kSizeScale(48);

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    TSFanyiFileSelectLanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TSFanyiFileSelectLanguageCell" forIndexPath:indexPath];

//    [cell showCell:self.inputModel];

    NSString *key = self.keyDatas[indexPath.section];

    NSArray*arr =self.dataDic[key];

    NSDictionary*dic = arr[indexPath.row];

    cell.nameLab.text= dic[@"name"];


    if((arr.count-1) == indexPath.row) {

        cell.lineView.hidden=YES;

    }else{

        cell.lineView.hidden=NO;

    }

    returncell;

}

/**返回右侧索引所包含的内容*/

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

//    NSMutableArray *sections = self.keyDatas;

    //往索引数组的开始处添加一个放大镜 放大镜是系统定义好的一个常量字符串表示UITableViewIndexSearch 当然除了放大镜外也可以添加其他文字

//    [sections insertObject:UITableViewIndexSearch  atIndex:0];

    return  self.keyDatas;


}

//点击右侧索引后跳转到的section

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {


    returnindex;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


}

#pragma mark业务处理

- (void)requestList {



    NSDictionary *dic = [TSGetInfoTool getPathForResource:@"fanyi_language" ofType:@"json"];

//    NSDictionary *dic = [TSGetInfoTool getPathForResource:@"fanyi_languageWd" ofType:@"json"];

    NSArray*languages = dic[@"language"];


    [selfdataAssembly:languages];

}

- (void)dataAssembly:(NSArray *)languages {


    NSMutableDictionary *dataDic = [[NSMutableDictionary alloc] init];

    for(NSDictionary*dicinlanguages) {

        NSString*name = dic[@"name"];

        NSString*firstKey = [selffirstCharactor:name];


        NSMutableArray*arr = [dataDicobjectForKey:firstKey];

        if(arr && arr.count>0) {

            [arraddObject:dic];

        }else{

            NSMutableArray *narr = [[NSMutableArray alloc] initWithObjects:dic, nil];

            [dataDicsetObject:narrforKey:firstKey];

        }

    }

    self.dataDic= dataDic;




    NSArray*keys = [dataDicallKeys];

    self.keyDatas = [keys sortedArrayUsingComparator:^NSComparisonResult(NSString* obj1, NSString* obj2) {


        return[obj1compare:obj2];

    }];


    [self.tabView reloadData];

}

//获取拼音首字母(传入汉字字符串, 返回大写拼音首字母)

- (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];

}

#pragma mark懒加载

- (UIView *)bgView {

    if(!_bgView) {

        _bgView = [[UIView alloc] init];

        _bgView.backgroundColor = [UIColor hex:@"#FFFFFF"];

        [_bgView setCornerRadius:16];

    }

    return _bgView;

}

- (UIView *)closeView {

    if (!_closeView) {

        _closeView = [[UILabel alloc] init];

        UIView*lineView = [[UIViewalloc]init];

        lineView.backgroundColor = [UIColor hex:@"#F0F2F4"];

        [lineViewsetCornerRadius:kSizeScale(3)];

        [_closeViewaddSubview:lineView];

        [lineViewmas_makeConstraints:^(MASConstraintMaker *make) {

            make.width.mas_equalTo(kSizeScale(32));

            make.height.mas_equalTo(kSizeScale(6));

            make.top.equalTo(_closeView).offset(kSizeScale(9));

            make.centerX.equalTo(_closeView);

        }];

        [_closeView setUserInteractionEnabled:YES];

    }

    return _closeView;

}

@end

你可能感兴趣的:(带右边索引的 UITableView)