簡單實現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

 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM