UITableView 自帶索引捏卓,一直用第三方的控件也沒(méi)有認(rèn)真研究,今天又碰到了類似問(wèn)題慈格,于是專門(mén)研究了下詳細(xì)的內(nèi)容怠晴,記錄于此。
- 1.創(chuàng)建TableView
@property (strong, nonatomic) UITableView *tableView;
//需添加代理和數(shù)據(jù)源
- 2.隨機(jī)生成一些基礎(chǔ)數(shù)據(jù)
NSArray *xings = @[@"趙",@"錢(qián)",@"孫",@"李",@"周",@"吳",@"鄭",@"王",@"馮",@"陳",@"楚",@"衛(wèi)",@"沈",@"殺",@"韓",@"楊"];
NSArray *ming1 = @[@"大",@"美",@"帥",@"應(yīng)",@"超",@"海",@"江",@"湖",@"春",@"夏",@"秋",@"冬",@"上",@"左",@"有",@"純"];
NSArray *ming2 = @[@"強(qiáng)",@"好",@"領(lǐng)",@"亮",@"超",@"華",@"奎",@"海",@"工",@"青",@"紅",@"潮",@"兵",@"垂",@"剛",@"山"];
for (int i = 0; i < count; i++) {
NSString *name = xings[arc4random_uniform((int)xings.count)];
NSString *ming = ming1[arc4random_uniform((int)ming1.count)];
name = [name stringByAppendingString:ming];
if (arc4random_uniform(10) > 3) {
NSString *ming = ming2[arc4random_uniform((int)ming2.count)];
name = [name stringByAppendingString:ming];
}
//數(shù)據(jù)模型進(jìn)行存儲(chǔ)
SDContactModel *model = [SDContactModel new];
model.name = name;
model.imageName = [SDAnalogDataGenerator randomIconImageName];
[self.dataArray addObject:model];
}
- 3.關(guān)鍵排序
_contacts = [self arrayForSections:objects];
[_tableView reloadData];
- (NSArray *)arrayForSections:(NSArray *)objects {
SEL selector = @selector(name);
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
// sectionTitlesCount 27 , A - Z + #
// section number
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
// Create 27 sections' data
NSMutableArray *mutableSections = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
[mutableSections addObject:[NSMutableArray array]];
}
// Insert records
for (id object in objects) {
NSInteger sectionNumber = [collation sectionForObject:object collationStringSelector:selector];
[[mutableSections objectAtIndex:sectionNumber] addObject:object];
}
// sort in section
for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
NSArray *objectsForSection = [mutableSections objectAtIndex:idx];
[mutableSections replaceObjectAtIndex:idx withObject:[[UILocalizedIndexedCollation currentCollation] sortedArrayFromArray:objectsForSection collationStringSelector:selector]];
}
// Remove empty sections && insert table data
NSMutableArray *existTitleSections = [NSMutableArray array];
for (NSArray *section in mutableSections) {
if ([section count] > 0) {
[existTitleSections addObject:section];
}
}
// Remove empty sections Index in indexList
NSMutableArray *existTitles = [NSMutableArray array];
NSArray *allSections = [collation sectionIndexTitles];
for (NSUInteger i = 0; i < [allSections count]; i++) {
if ([mutableSections[ i ] count] > 0) {
[existTitles addObject:allSections[ i ]];
}
}
//create indexlist data array
self.indexList = existTitles;
return existTitleSections;
}
- 4.TableView右側(cè)索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return _indexList;
}
5.索引小結(jié)
索引排序關(guān)鍵是用UILocalizedIndexedCollation來(lái)做分組和排序浴捆,也可以自定義方法進(jìn)行拼音排序蒜田,但不管什么方法,要遵循section和row的角色选泻,將同一個(gè)字母開(kāi)頭的數(shù)據(jù)存儲(chǔ)到一個(gè)section中冲粤,在section中以row的字母開(kāi)頭進(jìn)行排序美莫,最后保存索引列表,根據(jù)TableView右側(cè)索引添加方法進(jìn)行添加設(shè)置梯捕。6.附上傳送門(mén)
https://github.com/earthX/TableViewIndexDemo