This article shares the specific code for adding letter indexes to iOS tabview for your reference. The specific content is as follows
Article reprinted fromThe master source code portal
1. Convert Chinese characters to first letters
//The system gets the first letter- (NSString *) pinyinFirstLetter:(NSString*)sourceString { NSMutableString *source = [sourceString mutableCopy]; CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO); CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//This line is de-toned return source; }
2. Methods to bind to tabview
#import "" #import "" #import "" @interface ViewController (){ NSMutableArray<Person *> *dataArray; } //Array of pinyin initial letters after sorting@property(nonatomic,strong)NSMutableArray *indexArray; //Sorted result array@property(nonatomic,strong)NSMutableArray *letterResultArr; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //Simulate data loading to get Person's array in dataArray [self loadData]; //BMChineseSort file contains two array sorting functions for cell data and letters to the right //Sort Person array by Chinese according to the name attribute of the Person object //The data of each cell is sorted = [BMChineseSort IndexWithArray:dataArray Key:@"name"]; //The alphabet array on the left has been sorted = [BMChineseSort sortObjectArray:dataArray Key:@"name"]; UITableView *table = [[UITableView alloc] initWithFrame:]; = self; = self; [ addSubview:table]; } //Load simulated data-(void)loadData{ NSArray *stringsToSort=[NSArray arrayWithObjects: @"Li Bai",@"Zhang San", @"Chongqing",@"weight", @"adjust",@"Call", @"noob",@"Xiao Ming",@"Qianjue", @"Wang Jiaju", @"mouse",@"hello",@"How beautiful",@"KFC",@"##", nil]; //Simulate the array object received by the network request Person array dataArray = [[NSMutableArray alloc] initWithCapacity:0]; for (int i = 0; i<[stringsToSort count]; i++) { Person *p = [[Person alloc] init]; = [stringsToSort objectAtIndex:i]; = i; [dataArray addObject:p]; } } #pragma mark - UITableView - //section's titleHeader- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [ objectAtIndex:section]; } //section row count-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [ count]; } //The number of sections per group- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [[ objectAtIndex:section] count]; } //The index array on the right side of section-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return ; } //Call the corresponding relationship between the index and section when clicking on the index entry on the right- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ return index; } //Return to cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; } //Get the corresponding Person object <Replace with your own model object> Person *p = [[ objectAtIndex:] objectAtIndex:]; = ; return cell; } @end
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.