国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS tabview如何添加字母索引

2020-07-26 02:54:26
字體:
來源:轉載
供稿:網友

本文實例為大家分享了iOS tabview添加字母索引的具體代碼,供大家參考,具體內容如下

文章轉載自大神源碼傳送門

1、將漢字轉換成首字母

//系統獲取首字母- (NSString *) pinyinFirstLetter:(NSString*)sourceString { NSMutableString *source = [sourceString mutableCopy]; CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO); CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//這一行是去聲調的 return source;}

2、和tabview綁定的方法

#import "ViewController.h"#import "BMChineseSort.h"#import "Person.h"@interface ViewController (){ NSMutableArray<Person *> *dataArray;}//排序后的出現過的拼音首字母數組@property(nonatomic,strong)NSMutableArray *indexArray;//排序好的結果數組@property(nonatomic,strong)NSMutableArray *letterResultArr;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //模擬數據加載 dataArray中得到Person的數組 [self loadData]; //BMChineseSort 文件包含兩個對單元格數據和右側字母的數組排序函數 //根據Person對象的 name 屬性 按中文 對 Person數組 排序 //每一個單元格的數據,排序好了的 self.indexArray = [BMChineseSort IndexWithArray:dataArray Key:@"name"]; //左側的字母數組,已經排序好了 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];}//加載模擬數據-(void)loadData{ NSArray *stringsToSort=[NSArray arrayWithObjects:       @"李白",@"張三",       @"重慶",@"重量",       @"調節",@"調用",       @"小白",@"小明",@"千玨",       @"黃家駒", @"鼠標",@"hello",@"多美麗",@"肯德基",@"##",       nil]; //模擬網絡請求接收到的數組對象 Person數組 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的titleHeader- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [self.indexArray objectAtIndex:section];}//section行數-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [self.indexArray count];}//每組section個數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [[self.letterResultArr objectAtIndex:section] count];}//section右側index數組-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return self.indexArray;}//點擊右側索引表項時調用 索引與section的對應關系- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ return index;}//返回cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; if (cell == nil){  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; } //獲得對應的Person對象<替換為你自己的model對象> Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; cell.textLabel.text = p.name; return cell;}@end

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 饶阳县| 民和| 连州市| 瑞昌市| 开鲁县| 康定县| 杭锦后旗| 白玉县| 云南省| 商洛市| 彩票| 阜宁县| 北票市| 叶城县| 波密县| 邵阳县| 仙居县| 桃园县| 弋阳县| 武威市| 苗栗市| 泽普县| 饶阳县| 淮滨县| 兴隆县| 云浮市| 县级市| 余干县| 兴国县| 嵊泗县| 呼玛县| 瑞金市| 类乌齐县| 普定县| 烟台市| 读书| 开化县| 阿鲁科尔沁旗| 龙泉市| 绥滨县| 岢岚县|