iOS tabview如何添加字母索引
更新時間:2017年03月08日 14:00:16 作者:書弋江山
這篇文章主要為大家詳細介紹了iOS tabview如何添加字母索引,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS tabview添加字母索引的具體代碼,供大家參考,具體內(nèi)容如下
文章轉(zhuǎn)載自大神源碼傳送門
1、將漢字轉(zhuǎn)換成首字母
//系統(tǒng)獲取首字母
- (NSString *) pinyinFirstLetter:(NSString*)sourceString {
NSMutableString *source = [sourceString mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//這一行是去聲調(diào)的
return source;
}
2、和tabview綁定的方法
#import "ViewController.h"
#import "BMChineseSort.h"
#import "Person.h"
@interface ViewController (){
NSMutableArray<Person *> *dataArray;
}
//排序后的出現(xiàn)過的拼音首字母數(shù)組
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的結(jié)果數(shù)組
@property(nonatomic,strong)NSMutableArray *letterResultArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//模擬數(shù)據(jù)加載 dataArray中得到Person的數(shù)組
[self loadData];
//BMChineseSort 文件包含兩個對單元格數(shù)據(jù)和右側(cè)字母的數(shù)組排序函數(shù)
//根據(jù)Person對象的 name 屬性 按中文 對 Person數(shù)組 排序
//每一個單元格的數(shù)據(jù),排序好了的
self.indexArray = [BMChineseSort IndexWithArray:dataArray Key:@"name"];
//左側(cè)的字母數(shù)組,已經(jīng)排序好了
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];
}
//加載模擬數(shù)據(jù)
-(void)loadData{
NSArray *stringsToSort=[NSArray arrayWithObjects:
@"李白",@"張三",
@"重慶",@"重量",
@"調(diào)節(jié)",@"調(diào)用",
@"小白",@"小明",@"千玨",
@"黃家駒", @"鼠標",@"hello",@"多美麗",@"肯德基",@"##",
nil];
//模擬網(wǎng)絡(luò)請求接收到的數(shù)組對象 Person數(shù)組
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行數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.indexArray count];
}
//每組section個數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.letterResultArr objectAtIndex:section] count];
}
//section右側(cè)index數(shù)組
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.indexArray;
}
//點擊右側(cè)索引表項時調(diào)用 索引與section的對應(yīng)關(guān)系
- (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"];
}
//獲得對應(yīng)的Person對象<替換為你自己的model對象>
Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = p.name;
return cell;
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)
這篇文章主要介紹了淺談iOS開發(fā)如何適配暗黑模式(Dark Mode),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

