IOS uses UILocalizedIndexedCollation smart packet sorting

Here are some steps to use UILocalizedIndexedCollation

1, first of all, create a new model class, for example: I’ll create a new Person class. For simplicity, I’ll add a name attribute. Back to your Controller inside the control initialization, here will not say more.

2, initialize the array of data sources, such as: initialize an array of index headers, an array of section under each index, and a data source array, as shown below.

IOS uses UILocalizedIndexedCollation smart packet sorting
array classification

A new local array as a data source, for example: NSArray *localArray = @[@ @ “Lin Yu”, “Lin Hongwei”, “Jay Chou”, “@ @ @ Zhou Shuren”, “Zhou Jie”, “A Hua”, “@ @ @” pear “, Ann winter”, “Yang Guodong”, “@ @ Zhao Hua,” Ye Qing “,” @ @ @ “Wang Xiaomin”, “party dumping”, “Wu Xiaohui”, “@ @ @ Ren love”, “Chen Geng”, “Fu Haibo”, “@ @ @ Dubai”, “drop” @ “Heqing”, “Jess”, “@ @ Jie Bin” @ “sheep”;

The for loop then adds and evaluates the name property of the Person class and saves it to the data array, such as the following code:

For (int, i=0; i<
; localArray.count; i++) {

Person *model = [Person new];

Model.name = localArray[i];

[self.dataArray addObject:model];

}

3. Start the UILocalizedIndexedCollation initialization below.

UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];

/ / index
for collation

NSUInteger sectionNumb = [[collation, sectionTitles], count];

NSMutableArray *sectionArray = [[NSMutableArray, alloc], init];

/ / cycle index, initialize empty array and add to the data array

For (NSInteger = index = 0; index< sectionNumb; index++) {

[sectionArray addObject:[[NSMutableArray alloc] init]];

}

/ / 1. convenience of elements in the array

2. / acquisition of name value in the first letter of the 26 letters of the position

The 3. array / / model objects added to the corresponding letter of

For (Person, *model, in, self.dataArray) {

NSUInteger sectionIndex = [collation sectionForObject:model collationStringSelector:@selector (name)];

[sectionArray[sectionIndex] addObject:model];

}

/ / array in each section according to the name attribute retrieval ranking (
index)

1. / gets each array under section

2. for each array / / section of the alphabet

For (NSInteger index=0; index< sectionNumb; index++) {

NSMutableArray *personArrayForSection = sectionArray[index];

NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector (name)];

SectionArray[index] = sortedPersonArrayForSection;

}

Create an array of temp / / empty (when the purpose is to call the enumerateObjectsUsingBlock function to add to the empty array array, when the data source is an empty array removed, or in the function call to judge, empty removed)

NSMutableArray *temp = [NSMutableArray new];

[sectionArray enumerateObjectsUsingBlock:^ (NSArray, *arr, NSUInteger, IDX, BOOL, *stop) {

If (arr.count = = 0) {

[temp addObject:arr];

} else {

[self.sectionTitlesArray addObject:[collation sectionTitles][idx]];

}

}];

[sectionArray removeObjectsInArray:temp];

Self.sectionArray = sectionArray;

4, agent matters

#pragma mark search returns the number of Title arrays

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

Return self.sectionTitlesArray.count;

}

#pragma mark returns how many data each group has

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

Return [self.sectionArray[section] count];

}

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

Static NSString *ID = @ Identifier”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

If ({cell) {

Cell = [[UITableViewCell, alloc], initWithStyle:UITableViewCellStyleDefault, reuseIdentifier:ID];

}

NSUInteger section = indexPath.section;

NSUInteger row = indexPath.row;

Person *model = self.sectionArray[section][row];

Cell.textLabel.text = model.name;

Cell.accessoryType = UITableViewCellAccessoryNone;

Return cell;

}

In order to display the tableview / HeaderInSection title

– (NSString *) tableView: (* UITableView) tableView titleForHeaderInSection: (NSInteger) section{

Return [self.sectionTitlesArray objectAtIndex:section];

}

In order to display letters (/ / retrieval does not return the right index content is not displayed)

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

Return self.sectionTitlesArray;

}

Click on the right / / proxy method index, can be displayed when the index finger sliding set here the title (sectionIndexColor sectionIndexBackgroundColor is set the font color, background color)

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

Self.titleLabel.hidden = NO;

Self.titleLabel.text = title;

NSString *key = [self.sectionTitlesArray objectAtIndex:index];

For (UIView *view in tableView.subviews) {

NSLog (@ _________%@, view);

If ([view isKindOfClass:NSClassFromString (@ UITableViewIndex) {])

}

}

Return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];

}

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

Return 60;

}

– – (CGFloat) tableView: (UITableView *) tableView, heightForHeaderInSection: (NSInteger) section{

Return 35;

}

Done with this, the UILocalizedIndexedCollation smart grouping has been sorted, and the styles are as follows.

IOS uses UILocalizedIndexedCollation smart packet sorting

1, first of all, create a new model class, for example: I’ll create a new Person class. For simplicity, I’ll add a name attribute. Back to your Controller inside the control initialization, here will not say more.

    }

}

     return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];

}

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

     return 60;

}

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

     return 35;

}

做完这些就已经完成UILocalizedIndexedCollation智能分组排序了,样式就如下效果图。

IOS uses UILocalizedIndexedCollation smart packet sorting