废话不多说上代码 直接使用该方法就可以
- (void)allDataRanger {
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
for (WDCar *model in _dataSource) {
NSInteger sectionNumber = [collation sectionForObject:model collationStringSelector:@selector(name)];
NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
[sectionNames addObject:model];
}
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *personArrayForSection = newSectionsArray[index];
NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
newSectionsArray[index] = sortedPersonArrayForSection;
}
NSMutableArray *temp = [NSMutableArray new];
_indexDataSource = [NSMutableArray new];
[newSectionsArray enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL *stop) {
if (arr.count == 0) {
[temp addObject:arr];
} else {
[_indexDataSource addObject:[collation sectionTitles][idx]];
}
}];
_allDataSource =[[NSMutableArray alloc]init];
[newSectionsArray removeObjectsInArray:temp];
[_allDataSource removeAllObjects];
[_allDataSource addObjectsFromArray:newSectionsArray];