iOS tabview如何添加字母索引
更新時間:2017年03月08日 14:00:16 作者:書弋江山
這篇文章主要為大家詳細(xì)介紹了iOS tabview如何添加字母索引,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(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)過的拼音首字母數(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)用",
@"小白",@"小明",@"千玨",
@"黃家駒", @"鼠標(biā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;
}
//點(diǎn)擊右側(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ā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法
這篇文章主要介紹了iOS開發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法,包括對控制器聲明周期的控制介紹,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時傳輸
這篇文章主要為大家詳細(xì)介紹了iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時傳輸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
iOS開發(fā)中音頻視頻播放的簡單實(shí)現(xiàn)方法
視頻音頻是我們在ios日常開發(fā)中經(jīng)常會遇到的一個需求,所以下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中音頻視頻播放的簡單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
NSURLSession跨域重定向透傳HTTP Header問題解決
這篇文章主要為大家介紹了NSURLSession跨域重定向透傳HTTP Header問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

