IOS uilocalizedindexedcollaboration sorting artifact

Uilocalizedindexedcollaboration is a class that helps us organize list data. It can generate index titles of corresponding regions according to regions. Instead of creating its object directly, we can get a single object corresponding to the current region through UI localized indexedcollaboration + currentcollation.

Project address: https://github.com/MisterZhouZhou/UISearchControllerAndUILocalizedCollection

The simple operation is as follows:

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

    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = @"cell";
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
               atIndex:(NSInteger)index
{
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

Design sketch:

Next, we start to sort the Chinese surnames:
1. Initialization data:

//Configure data sources
    NSArray *firstNameArray = @[@"Zhao",@"money",@"Grandchildren",@"Plum",@"week",@"Wu",@"Zheng",@"king",@"outer wall or surrounding area of a city",@"pine",@"Song Dynasty",@"long",@"large",@"Small"];
    NSMutableArray *tempArray = [NSMutableArray array];
    for (int i=0; i<firstNameArray.count; i++) {
        Person *p = [Person new];
        p.name = [NSString stringWithFormat:@"%@",firstNameArray[i]];
        [tempArray addObject:p];
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. Sort and filter data

    //Initialize uilocalizedindexedcollaboration
    localizedCollection = [UILocalizedIndexedCollation currentCollation];
    //To get the number of collation indexes, here are 27 (26 letters and 1 ×)
    NSInteger sectionTitlesCount = [[localizedCollection sectionTitles] count];
    //Initializes an array, newSectionsArray, to hold the final data
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    //Initialize 27 empty arrays and add them to newSectionsArray
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
            NSMutableArray *array = [[NSMutableArray alloc] init];
            [newSectionsArray addObject:array];
    }
    //Divide everyone under a section by name
    for (Person *temp in tempArray) {
        //Get the location of the value of the name attribute, such as "lindan". The initial is L. rank 11 in A-Z (the first digit is 0), and the sectionNumber is 11
        NSInteger sectionNumber = [localizedCollection sectionForObject:temp collationStringSelector:@selector(name)];
        NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
        [sectionNames addObject:temp];
    }

    //Sort the arrays in each section according to the name attribute
    for (int index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *personArrayForSection = newSectionsArray[index];
        NSArray *sortedPersonArrayForSection = [localizedCollection sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
        newSectionsArray[index] = sortedPersonArrayForSection;
    }


    //section title
    sectionTitleArray = [NSMutableArray array];
    NSMutableArray *tempArr = [NSMutableArray array];
    [newSectionsArray enumerateObjectsUsingBlock:^(NSArray *array, NSUInteger idx, BOOL * _Nonnull stop) {

        if (array.count == 0) {
            [tempArr addObject:array];
        }else{
            [sectionTitleArray addObject:[localizedCollection sectionTitles][idx]];
        }
    }];
    [newSectionsArray removeObjectsInArray:tempArr];

    dataArray = newSectionsArray.copy;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

Design sketch:

Tags: Attribute github

Posted on Wed, 01 Apr 2020 16:13:09 -0700 by cayman_d