Sag*_*ari 1 iphone indexing xcode objective-c uitableview
在iPhone原生电话簿中 - 顶部有一个搜索字符,底部有#字符.
我想在我的表索引中添加这两个字符.
目前我已经实现了以下代码.
atoz=[[NSMutableArray alloc] init];
for(int i=0;i<26;i++){
[atoz addObject:[NSString stringWithFormat:@"%c",i+65]];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return atoz;
}
Run Code Online (Sandbox Code Playgroud)
如何在我的UITableView中拥有#字符和搜索符号?
解决这个问题的最佳方法是利用框架提供的工具.在这种情况下,您要使用UILocalizedIndexedCollation(开发人员链接).
我还有一个这个类的装饰器,旨在为您插入{{search}}图标并处理偏移.它是UILocalizedIndexedCollation的类似替代品.
我已经在我的博客上发布了更深入的如何使用它的说明.该装饰可在这里(GIST).
基本思想是将您的集合分组为一个数组数组,每个数组代表一个部分.您可以使用UILocalizedIndexedCollation(或我的替代品)来执行此操作.这是NSArray我用来做这个的小类别方法:
@implementation NSArray (Indexing)
- (NSArray *)indexUsingCollation:(UILocalizedIndexedCollation *)collation withSelector:(SEL)selector;
{
NSMutableArray *indexedCollection;
NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];
indexedCollection = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[indexedCollection addObject:array];
[array release];
}
// Segregate the data into the appropriate section
for (id object in self) {
NSInteger sectionNumber = [collation sectionForObject:object collationStringSelector:selector];
[[indexedCollection objectAtIndex:sectionNumber] addObject:object];
}
// Now that all the data's in place, each section array needs to be sorted.
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *arrayForSection = [indexedCollection objectAtIndex:index];
NSArray *sortedArray = [collation sortedArrayFromArray:arrayForSection collationStringSelector:selector];
[indexedCollection replaceObjectAtIndex:index withObject:sortedArray];
}
NSArray *immutableCollection = [indexedCollection copy];
[indexedCollection release];
return [immutableCollection autorelease];
}
@end
Run Code Online (Sandbox Code Playgroud)
所以,给定一个对象数组,例如books我想根据它们的名称分成几个部分(Book该类有一个name方法),我会这样做:
NSArray *books = [self getBooks]; // etc...
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSArray *indexedBooks = [books indexUsingCollation:collation withSelector:@selector(name)];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1605 次 |
| 最近记录: |