这篇文章是我在看过其英文档后写得。。。。 UILocalizedIndexedCollation 是相当于是tableView的一个辅助类,用来对tableView的数据源进行管理,排序,定位。 UILoaclizedIndexedCollation 提供了对tableView的section titles 和section index titles的配置。带有section index的tableView,主要是应用于很多个分类的数据项,例如电话本,联系人的姓的首字母相同时会分在一个组下面,当联系人很多的时候,要寻找一个人就很麻烦了,这个时候边搜索的优势就体现出来了,当你点击section index 中得一个title,它就会跳到跟这个section index对应的section处。
现在我们要为tableView有个section index做准备。
1.首先创建一个UILocalizedIndexedCollation对象,它是个shared instance,所以使用
UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
2.使用创建的theCollation调用sectionForObject:collationStringSelector:这个方法决定了每个对象属于哪个section,并返回一个NSInteger值来标识这个section
for (AddressBookEntity *addressBook in addressBookTemp) {
NSInteger sect = [theCollation sectionForObject:addressBook
collationStringSelector:@selector(getFirstName)];
addressBook.sectionNumber = sect;//记录下对象的section id
}
其中决定对象属于哪个section是由getFirstName方法来决定的,这个方法是由自己实现的,后面我会贴出它的实现代码。当我们得到对象的section id时,我们最好记录下来,供后面获取到整个section,从而对seciton内的数据进行排序。
3.创建存储所有section的array,以及为每个section分配它的存储空间,初始空间为1。
NSInteger highSection = [[theCollation sectionTitles] count];
NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
for (int i=0; i<=highSection; i++) {
NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
[sectionArrays addObject:sectionArray];
}
利用数据源来填充每个section ,根据对象的section id 把每个对象放入其对应的section array里
for (AddressBookEntity *addressBook in addressBookTemp) {
[(NSMutableArray *)[sectionArrays objectAtIndex:addressBook.sectionNumber] addObject:addressBook];
}
4.用theCollation调用 sortedArrayFromArray:collationStringSelector: 这个方法对每个section里面的数据进行排序,然后返回一个已经排好序的array,将其添加到一个全局变量listContent中。
for (NSMutableArray *sectionArray in sectionArrays) {
NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getFirstName)];
[listContent addObject:sortedSection];
}
至此为止,我们就已经将tableView的数据源进行了分组,确定了每个对象所在的section,以及每个section下得所有对象的排序,这个功能可以很方便的对tableView的数据进行排序,可以作为一个单独的功能来使用。我不实现
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
这个三个代理方法就不会显示section和section index,但是我显示的数据就全是排好序的。很方便的
当然如果你想显示section和section index,就需要实现上面的这个三个方法,这三个方法可以直接用UILocalizedIndexedCollation来实现,如下:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
//返回section index对应的section
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
note:section title 和section index title 是可以不相同的。
这是我第一次写这种文章,肯定有很多说得不太得当的地方,请多谅解,多谢多谢。
以下是getFirstName的具体内容,我觉得比较实用。
#import "AddressBookEntity.h"
@implementation AddressBookEntity
@synt