iOS tabview How to Add an Alphabetic Index
- 2021-11-14 07:19:18
- OfStack
In this article, we share the specific code of iOS tabview to add letter index for your reference, the specific contents are as follows
The article is reproduced from the Great God source code portal
1. Convert Chinese characters into initials
// The system gets the initials
- (NSString *) pinyinFirstLetter:(NSString*)sourceString {
NSMutableString *source = [sourceString mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);// This 1 The line is tone-free
return source;
}
2. Method of binding with tabview
#import "ViewController.h"
#import "BMChineseSort.h"
#import "Person.h"
@interface ViewController (){
NSMutableArray<Person *> *dataArray;
}
// Array of the first letters of Pinyin after sorting
@property(nonatomic,strong)NSMutableArray *indexArray;
// Array of sorted results
@property(nonatomic,strong)NSMutableArray *letterResultArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Analog data loading dataArray Get from Person Array of
[self loadData];
//BMChineseSort The file contains two array sorting functions for cell data and right-hand letters
// According to Person Object's name Attribute By Chinese Right Person Array Sort
// Every 1 Cells of data, sorted
self.indexArray = [BMChineseSort IndexWithArray:dataArray Key:@"name"];
// The letter array on the left has been sorted
self.letterResultArr = [BMChineseSort sortObjectArray:dataArray Key:@"name"];
UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
// Load simulation data
-(void)loadData{
NSArray *stringsToSort=[NSArray arrayWithObjects:
@" Li Bai ",@" Zhang 3",
@" Chongqing ",@" Weight ",
@" Adjustment ",@" Call ",
@" Xiaobai ",@" Xiao Ming ",@" Kindred ",
@" Ka Kui Wong ", @" Mouse ",@"hello",@" How beautiful ",@" Kentucky Fried Chicken ",@"##",
nil];
// Mimics an array object received by a network request Person Array
dataArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i<[stringsToSort count]; i++) {
Person *p = [[Person alloc] init];
p.name = [stringsToSort objectAtIndex:i];
p.number = i;
[dataArray addObject:p];
}
}
#pragma mark - UITableView -
//section Adj. titleHeader
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.indexArray objectAtIndex:section];
}
//section Number of rows
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.indexArray count];
}
// Each group section Number
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.letterResultArr objectAtIndex:section] count];
}
//section Right side index Array
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.indexArray;
}
// Called when the index entry on the right side is clicked Index and section Correspondence of
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}
// Return cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
// Obtain the corresponding Person Object < Replace it with your own model Object >
Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = p.name;
return cell;
}
@end