13.UILocalizedIndexedCollation

作者: 二斤寂寞 | 来源:发表于2020-08-05 11:03 被阅读0次

简介

本地化下按首字母分组排序的神器——UILocalizedIndexedCollation

直接上demo

a_1.gif

1、创建一个person模型

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Person : NSObject

@property(nonatomic, strong) NSString *name;

- (instancetype)initWithName:(NSString *)name;
@end

NS_ASSUME_NONNULL_END
#import "Person.h"

@implementation Person

- (instancetype)initWithName:(NSString *)name {
    if (self = [super init]) {
        self.name = name;
    }
    return self;
}
@end

2、构造数据

- (void)loadData {
    
    NSArray *testArr = @[@"杨幂",@"李易峰",@"王祖蓝", @"垃圾", @"苏有朋", @"大海", @"佟丽娅", @"霍建华", @"薛之谦", @"周笔畅", @"李宇春", @"张靓颖", @"阿里郎", @"胡一天", @"贾乃亮", @" 罗晋"];
    
    NSMutableArray *personArr = [NSMutableArray arrayWithCapacity:testArr.count];
    for (NSString *str in testArr) {
        Person *person = [[Person alloc] initWithName:str];
        [personArr addObject:person];
    }
    
    self.collation = [UILocalizedIndexedCollation currentCollation];
    
    //1.获取获取section标题
    NSArray *titles = self.collation.sectionTitles;
    
    //2.构建每个section数组
    NSMutableArray *secionArray = [NSMutableArray arrayWithCapacity:titles.count];
    for (int i = 0; i < titles.count; i++) {
        NSMutableArray *subArr = [NSMutableArray array];
        [secionArray addObject:subArr];
    }
    
    //3.排序
    //3.1 按照将需要排序的对象放入到对应分区数组
    for (Person *person in personArr) {
        NSInteger section = [self.collation sectionForObject:person collationStringSelector:@selector(name)];
        NSMutableArray *subArr = secionArray[section];
        
        [subArr addObject:person];
    }
    
    //3.2 分别对分区进行排序
    for (NSMutableArray *subArr in secionArray) {
        NSArray *sortArr = [self.collation sortedArrayFromArray:subArr collationStringSelector:@selector(name)];
        [subArr removeAllObjects];
        [subArr addObjectsFromArray:sortArr];
    }
    
    //修改数据源
    self.dataSource = [NSArray arrayWithArray:secionArray];
}

3、添加数据到tableview上

#pragma mark - UITableViewDataSource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[self.collation sectionTitles] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self.collation sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [self.collation sectionForSectionIndexTitleAtIndex:index];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.collation.sectionTitles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    Person *person = [self.dataSource[indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = person.name;
    return cell;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.tipLabel.frame.size.height + 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor blueColor];
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    return _tableView;
}

相关文章

网友评论

    本文标题:13.UILocalizedIndexedCollation

    本文链接:https://www.haomeiwen.com/subject/onnfxktx.html