日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗

發(fā)布時(shí)間:2024/7/19 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

UITableView索引功能是常見(jiàn)的,主要是獲取中英文的首字母并排序,系統(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是別人寫的獲取首字母并對(duì)首字母進(jìn)行字典分類的NSString Categrory,這樣大大簡(jiǎn)化了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 = @[@"登記", @"大奔", @"周傅", @"愛(ài)德華",@"((((", @"啦文琪羊", @" s文強(qiáng)", @"過(guò)段時(shí)間", @"等等", @"各個(gè)", @"宵夜**", @"***", @"貝爾",@"*而結(jié)婚*", @"返回***", @"你還", @"與非門*", @"是的", @"*模塊*", @"*沒(méi)做*",@"俄文", @" *#咳嗽", @"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標(biāo)題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頭視圖標(biāo)題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {NSDictionary *dict = self.dataArray[section];NSString *title = dict[@"firstLetter"];return title; }//添加索引欄標(biāo)題數(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; }//點(diǎn)擊索引欄標(biāo)題時(shí)執(zhí)行 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {//這里是為了指定索引index對(duì)應(yīng)的是哪個(gè)section的,默認(rèn)的話直接返回index就好。其他需要定制的就針對(duì)性處理if ([title isEqualToString:UITableViewIndexSearch]){[tableView setContentOffset:CGPointZero animated:NO];//tabview移至頂部return NSNotFound;}else{return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索標(biāo)識(shí) } }

?

Demo 下載 ?http://files.cnblogs.com/files/sixindev/PinyinIndexTableview.zip

轉(zhuǎn)載于:https://www.cnblogs.com/sixindev/p/4504578.html

總結(jié)

以上是生活随笔為你收集整理的简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。