iOS通讯录排序与显示

来源:互联网 发布:js 返回首页清除历史 编辑:程序博客网 时间:2024/06/07 03:05

参考http://my.oschina.net/u/868062/blog/205098

该网址可以下载pinyin.m和pinyin.h文件 ChineseString的代码也在上面




补充:在使用的过程中发现,如果原字符串中含有下划线这样一些特殊字符串会被删除过滤掉,所以我在ChineseString.m中把

+(NSMutableArray*)ReturnSortChineseArrar:(NSArray*)stringArr

中的该方法调用屏蔽了,不然我在使用的时候该字符串跟原字符串不一致会导致我的一些程序出错。

//这里我自己写了一个递归过滤指定字符串   RemoveSpecialCharacter(会去掉字符串里的一些特殊字符,如果要保留原字符串就把该方法屏蔽)

        

//        chineseString.string = [ChineseString RemoveSpecialCharacter:chineseString.string];

        // NSLog(@"string====%@",chineseString.string);





#import "MyFriendViewController.h"

#import "ChineseString.h"

#import "DetialOfFriendViewController.h"


@interface MyFriendViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    NSMutableArray * indexArray;

    NSMutableArray * letterResultArray;

}

@end


@implementation MyFriendViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.automaticallyAdjustsScrollViewInsets =YES;

    [selfinitDataSource];

    [selfinitInterFace];

}


- (void)initDataSource

{

    indexArray = [[NSMutableArray alloc] init];//引索数组

    letterResultArray = [[NSMutableArray alloc] init];//排序结果数组

    NSArray *stringsToSort=[NSArrayarrayWithObjects:

                            @"hhh, .$",@"王小东",@"开源中国 ",@"www.oschina.net",

                            @"df源技术",@"社区",@"chuang",@"传播",

                            @"2014",@"2013",@"on the way",@"中国",@"暑假作业",

                            @"键盘",@"just do it",@"hello",@"world",

                            nil];

    

    indexArray = [ChineseString IndexArray:stringsToSort];

    letterResultArray = [ChineseString LetterSortArray:stringsToSort];

}


- (void)initInterFace

{

    

    UITableView * contactsTableView = [[UITableView alloc] initWithFrame:self.view.frame];

    contactsTableView.backgroundColor = [UIColoryellowColor];

    contactsTableView.delegate =self;

    contactsTableView.dataSource =self;

    [self.view addSubview:contactsTableView];

}


#pragma mark - UITableViewDelegate

// SectionHeader的值

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString * key = indexArray[section];

    return key;

}

// Section header view

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UILabel *lab = [[UILabelalloc] init];

    lab.backgroundColor = [UIColorgrayColor];

    lab.text = indexArray[section];

    lab.textColor = [UIColorwhiteColor];

    return lab;

}

// row height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return50;

}


#pragma mark Table View Data Source Methods

//设置右方表格的索引数组

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return indexArray;

}

#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

{

    NSLog(@"title===%@",title);

    return index;

}


//  -允许数据源告知必须加载到Table View中的表的Section数。

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [indexArray count];

}

//  -设置表格的行数为数组的元素个数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [letterResultArray[section] count];

}

//  -每一行的内容为数组相应索引的值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    staticNSString * cellId = @"contactCell";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellId];

    if (cell ==nil)

    {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];

        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    }

    

    cell.textLabel.text = letterResultArray[indexPath.section][indexPath.row];

    return cell;

}

//  选中cell的操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"---->%@",letterResultArray[indexPath.section][indexPath.row]);

    

    DetialOfFriendViewController * detailFriendVC = [[DetialOfFriendViewController alloc] init];

    detailFriendVC.personImage = [UIImage imageNamed:@"heard3.jpg"];

    detailFriendVC.nameString = @"小刚";

    detailFriendVC.signatureString = @"I will good good study,day day up!";

    detailFriendVC.phoneNumString = @"18966379935";

    detailFriendVC.sexString = @"";

    detailFriendVC.addressString = @"深圳莆田";

    [self.navigationController pushViewController:detailFriendVC animated:NO];

}







0 0