简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗
生活随笔
收集整理的這篇文章主要介紹了
简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
UITableView索引功能是常見的,主要是獲取中英文的首字母并排序,系統(tǒng)自帶獲取首字母
//系統(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; }?
PinYin.h文件是網(wǎng)上比較常用的獲取中英文首字母方法,NSString+PinYin.h是別人寫的獲取首字母并對首字母進行字典分類的NSString Categrory,這樣大大簡化了ViewContorl里的代碼量
在只需一行就能獲得首字母分類排序后的數(shù)組
參考:http://rainbownight.blog.51cto.com/1336585/1368730
//導(dǎo)入 #import "NSString+PinYin.h" //索引數(shù)組 NSArray *indexArray= [array arrayWithPinYinFirstLetterFormat];?
主要代碼
#import "ViewController.h" #import "NSString+PinYin.h"@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>@property(nonatomic,strong) UITableView *myTableView; @property(nonatomic,strong) NSMutableArray *dataArray;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. [self initDataSource];[self createTableView]; }?
UI及數(shù)據(jù)源
#pragma mark----CreatMyCustomTablevIew----- - (void)createTableView {self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,20,self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];self.myTableView.delegate = self;self.myTableView.dataSource = self;[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"REUSE_CELLID"];self.myTableView.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);[self.view addSubview:self.myTableView];self.myTableView.sectionIndexColor =[UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:1.0]; self.myTableView.sectionIndexBackgroundColor=[UIColor clearColor];[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];UISearchBar *mSearchBar = [[UISearchBar alloc] init];mSearchBar.delegate = self;mSearchBar.placeholder = @"搜索";[mSearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];[mSearchBar sizeToFit];self.myTableView.tableHeaderView=mSearchBar; }- (void)initDataSource {NSArray *array = @[@"登記", @"大奔", @"周傅", @"愛德華",@"((((", @"啦文琪羊", @" s文強", @"過段時間", @"等等", @"各個", @"宵夜**", @"***", @"貝爾",@"*而結(jié)婚*", @"返回***", @"你還", @"與非門*", @"是的", @"*模塊*", @"*沒做*",@"俄文", @" *#咳嗽", @"6",@"fh",@"C羅",@"鄧肯"];self.dataArray =[NSMutableArray arrayWithArray:indexArray];[self.myTableView reloadData]; }?
TableView相關(guān)代理
#pragma mark--- UITableViewDataSource and UITableViewDelegate Methods--- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return [self.dataArray count]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if(section == 0){return 1;}else{NSDictionary *dict = self.dataArray[section];NSMutableArray *array = dict[@"content"];return [array count];} }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];NSDictionary *dict = self.dataArray[indexPath.section];NSMutableArray *array = dict[@"content"];cell.textLabel.text = array[indexPath.row];cell.textLabel.textColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:1.0];return cell; }- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {//自定義Header標題UIView* myView = [[UIView alloc] init];myView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];titleLabel.textColor=[UIColor whiteColor];NSString *title = self.dataArray[section][@"firstLetter"];titleLabel.text=title;[myView addSubview:titleLabel];return myView; }TableView索引欄相關(guān)設(shè)置
#pragma mark---tableView索引相關(guān)設(shè)置---- //添加TableView頭視圖標題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {NSDictionary *dict = self.dataArray[section];NSString *title = dict[@"firstLetter"];return title; }//添加索引欄標題數(shù)組 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {NSMutableArray *resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];for (NSDictionary *dict in self.dataArray) {NSString *title = dict[@"firstLetter"];[resultArray addObject:title];}return resultArray; }//點擊索引欄標題時執(zhí)行 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {//這里是為了指定索引index對應(yīng)的是哪個section的,默認的話直接返回index就好。其他需要定制的就針對性處理if ([title isEqualToString:UITableViewIndexSearch]){[tableView setContentOffset:CGPointZero animated:NO];//tabview移至頂部return NSNotFound;}else{return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索標識 } }?
Demo 下載 ?http://files.cnblogs.com/files/sixindev/PinyinIndexTableview.zip
轉(zhuǎn)載于:https://www.cnblogs.com/sixindev/p/4504578.html
總結(jié)
以上是生活随笔為你收集整理的简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: page.ClientScript.Re
- 下一篇: Openstack Havana的两个排