iOS tabview如何添加字母索引
更新時(shí)間:2017年03月08日 14:00:16 作者:書弋江山
這篇文章主要為大家詳細(xì)介紹了iOS tabview如何添加字母索引,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了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)過(guò)的拼音首字母數(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 文件包含兩個(gè)對(duì)單元格數(shù)據(jù)和右側(cè)字母的數(shù)組排序函數(shù)
//根據(jù)Person對(duì)象的 name 屬性 按中文 對(duì) Person數(shù)組 排序
//每一個(gè)單元格的數(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)用",
@"小白",@"小明",@"千玨",
@"黃家駒", @"鼠標(biāo)",@"hello",@"多美麗",@"肯德基",@"##",
nil];
//模擬網(wǎng)絡(luò)請(qǐng)求接收到的數(shù)組對(duì)象 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個(gè)數(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;
}
//點(diǎn)擊右側(cè)索引表項(xiàng)時(shí)調(diào)用 索引與section的對(duì)應(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"];
}
//獲得對(duì)應(yīng)的Person對(duì)象<替換為你自己的model對(duì)象>
Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = p.name;
return cell;
}
@end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談iOS開發(fā)如何適配暗黑模式(Dark Mode)
這篇文章主要介紹了淺談iOS開發(fā)如何適配暗黑模式(Dark Mode),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
iOS 高效的分頁(yè)加載實(shí)現(xiàn)示例
本篇文章主要介紹了iOS 高效的分頁(yè)加載實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
iOS 對(duì)plist文件進(jìn)行讀寫,增刪改查的實(shí)例
下面小編就為大家?guī)?lái)一篇iOS 對(duì)plist文件進(jìn)行讀寫,增刪改查的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
iOS實(shí)現(xiàn)自定義表單實(shí)例代碼
表單對(duì)大家來(lái)說(shuō)應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)自定義表單的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04

