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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

UITableView索引功能是常見的,主要是獲取中英文的首字母并排序,系統自帶獲取首字母

//系統獲取首字母 - (NSString *) pinyinFirstLetter:(NSString*)sourceString {NSMutableString *source = [sourceString mutableCopy];CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//這一行是去聲調的return source; }

?

PinYin.h文件是網上比較常用的獲取中英文首字母方法,NSString+PinYin.h是別人寫的獲取首字母并對首字母進行字典分類的NSString Categrory,這樣大大簡化了ViewContorl里的代碼量

在只需一行就能獲得首字母分類排序后的數組

參考:http://rainbownight.blog.51cto.com/1336585/1368730

//導入 #import "NSString+PinYin.h" //索引數組 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及數據源

#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文強", @"過段時間", @"等等", @"各個", @"宵夜**", @"***", @"貝爾",@"*而結婚*", @"返回***", @"你還", @"與非門*", @"是的", @"*模塊*", @"*沒做*",@"俄文", @" *#咳嗽", @"6",@"fh",@"C羅",@"鄧肯"];self.dataArray =[NSMutableArray arrayWithArray:indexArray];[self.myTableView reloadData]; }

?

TableView相關代理

#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索引欄相關設置

#pragma mark---tableView索引相關設置---- //添加TableView頭視圖標題 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {NSDictionary *dict = self.dataArray[section];NSString *title = dict[@"firstLetter"];return title; }//添加索引欄標題數組 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {NSMutableArray *resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];for (NSDictionary *dict in self.dataArray) {NSString *title = dict[@"firstLetter"];[resultArray addObject:title];}return resultArray; }//點擊索引欄標題時執行 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {//這里是為了指定索引index對應的是哪個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

轉載于:https://www.cnblogs.com/sixindev/p/4504578.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。