iOS开发之通信录

如今的软件基本都有通讯功能,因此作一个通信录是开发人员必备的一个技能,界面不算优美,可是大部分功能都有,咱们这里用了github上一个汉字转拼音的工具(https://github.com/c6357/YUChineseSorting)。
先说说这个工具,这个工具能够对一系列的汉字转成拼音,并返回索引与索引对应的数组。git

NSArray *stringsToSort = 
 [NSArray arrayWithObjects:@"¥捣乱, .$",@" ¥Chin ese ",@"我爱中国 ",@"github",@"开源",@"社区",@"技术",@"传播",@"2014",@"a1",@"1 00",@"中国",@"暑假做业",@"键盘", @"大哥",@"helloworld",@"oop",@"b1",nil];

    self.friendListMuArray = [stringsToSort copy];
    self.indexArray = [ChineseString        IndexArray:self.friendListMuArray];
    self.letterResultArr = [ChineseString  LetterSortArray:self.friendListMuArray];

咱们能够看到,他将一些相似于¥,.$等符号过滤了,还将空格也过滤了,最后返回了已经排好序的两个数组,indexArray是索引数组,letterResultArr是索引数组对应的数据,如索引为#,则该数组放置的内容为100和2014,咱们看看最后的效果图:
这里写图片描述github

我这的布局是一个searchBar,下面两个button,再下面是一个tableview,其实也能够将两个button也放进tableview里面,可是我不想这两个button也跟着tableview一块儿滑动,因此把他们放到外面。
tableview咱们应该是比较熟悉的一个控件,
设置代理与数据源:web

self.tableView.delegate=self;
self.tableView.dataSource=self;

设置节的数量:数组

//节数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.indexArray count];
}

设置节中row的数量:svg

//每节有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.letterResultArr objectAtIndex:section] count];
}

而后给tableview填充cell:工具

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

设置索引和节标题:oop

//右侧索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.indexArray;
}

//索引放在节标题上
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.indexArray objectAtIndex:section];
    return key;
}

点击右侧索引返回到对应的section:布局

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    [self showSectionTitle:title];
    return index;
}

这些基本都没有问题,可是最后发现一个有意思的地方:ui

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

一开始实现是:spa

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lab = [UILabel new];
    lab.backgroundColor = [UIColor groupTableViewBackgroundColor];
    lab.text = [self.indexArray objectAtIndex:section];
    lab.textColor = [UIColor blackColor];

    return lab;
}

后来发现text居左布局,贴在屏幕边上,一开始想固然的给label设置frame:
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 20, 20)];

结果丝毫改变都没有,后来看着方法名,想到view应该是要包裹整个header,因此若是label的宽度不够的话,会填充成屏幕的宽度,这个优先级比代码设置frame的优先级更高,因此作了以下改进:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *v = [UIView new];
    v.backgroundColor = [UIColor groupTableViewBackgroundColor];

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(25, 5, 20, 20)];
    lab.backgroundColor = [UIColor clearColor];
    lab.text = [self.indexArray objectAtIndex:section];
    lab.textColor = [UIColor blackColor];

    [v addSubview:lab];
    return v;
}

这样就能让label的text显示不那么靠边了。