我正在尝试对我的Facebook好友列表进行排序,以实现索引表视图,就像在Facebook Messenger中找到的那样。
我最初尝试使用UILocalizedIndexedCollation
,但我被困在“collationStringSelector
”上,因为我的dataSource数组是id<"FBGraphUser">
个对象,因此我没有可用于选择器的属性。关于如何实现这一点的任何想法(它不必使用这种方法,我对任何事情都开放!)?
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector {
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count]; // section count is take from sectionTitles and not sectionIndexTitles
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
// create an array to hold the data for each section
for (int i = 0; i < sectionCount; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
// put each object into a section
for (id object in array) {
NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
// sort each section
for (NSMutableArray *section in unsortedSections) {
[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
答案 0 :(得分:0)
这是我前段时间创建的UITableViewController的子类,用于制作索引表视图。我使用它作为控制台的基类,其表视图出现在屏幕上,因此它是一个可重用的类,可以完成创建表的艰苦工作。您可以按原样使用它,但我没有尝试使它太普遍。我传入一个简单的自定义对象数组(我的对象只有名字和姓氏),这个类创建了部分和索引。所以这是代码,
.h
@interface RDIndexedTableController : UITableViewController
@property (strong,nonatomic) NSArray *inputArray;
@property (strong,nonatomic) NSString *sectionKey;
@property (strong,nonatomic) NSString *secondarySortKey;
@property (nonatomic, retain) NSMutableArray *sectionsArray;
@property (nonatomic, retain) UILocalizedIndexedCollation *collation;
-(void)convertArray:(NSArray *)input usingSectionKey:(NSString *)key secondarySortKey:(NSString *)sorter;
@end
.m
@interface RDIndexedTableController ()
@property (strong,nonatomic) NSMutableArray *firstLetterArray;
@end
@implementation RDIndexedTableController
-(void)convertArray:(NSArray *)input usingSectionKey:(NSString *)key secondarySortKey:(NSString *)sorter {
self.inputArray = input;
self.sectionKey = key;
self.secondarySortKey = sorter;
[self configureSections];
}
- (void)configureSections {
self.collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionTitlesCount = [[self.collation sectionTitles] count];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (int index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
for (id obj in self.inputArray) {
NSInteger sectionNumber = [self.collation.sectionTitles indexOfObject:[[obj valueForKey:self.sectionKey] substringToIndex:1]];
NSMutableArray *sectionForObjects = [newSectionsArray objectAtIndex:sectionNumber];
[sectionForObjects addObject:obj];
}
for (int index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *objectArrayForSection = [newSectionsArray objectAtIndex:index];
NSArray *firstSortedObjectArrayForSection = [self.collation sortedArrayFromArray:objectArrayForSection collationStringSelector:NSSelectorFromString(self.secondarySortKey)];
NSArray *sortedObjectArrayForSection = [self.collation sortedArrayFromArray:firstSortedObjectArrayForSection collationStringSelector:NSSelectorFromString(self.sectionKey)];
[newSectionsArray replaceObjectAtIndex:index withObject:sortedObjectArrayForSection];
}
self.sectionsArray = newSectionsArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.collation.sectionTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *objectsInSection = [self.sectionsArray objectAtIndex:section];
return objectsInSection.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.collation.sectionTitles[section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([[self.sectionsArray objectAtIndex:section] count] == 0) {
return 0;
}else{
return 30;
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}
在应用程序中显示的表格视图控制器中,我只需要这么少量的代码来填充表格,
-(void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
NSMutableArray *mut = [NSMutableArray new];
// create the Person objects and add them to the array here
[self convertArray:mut usingSectionKey:@"lastName" secondarySortKey:@"firstName"];
[self.tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *first = [self.sectionsArray[indexPath.section][indexPath.row] valueForKey:@"firstName"];
NSString *last = [self.sectionsArray[indexPath.section][indexPath.row] valueForKey:@"lastName"];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",first,last];
return cell;
}