一个简化数组对象按字典序排序的类。
您可以将其用于将数组排序到序外观,适用于iOS中的电话、联系人、音乐等应用。
如果您在使用CocoaPods(建议这么做),将其添加到Podfile中
pod 'CGLAlphabetizer', '~> 0.1'
否则,您可以下载并将CGLAlphabetizer.h
和CGLAlphabetizer.m
文件从Classes
文件夹中拖入。
CGLAlphabetizer对象接受一个对象数组和一个用于排序的关键路径,并返回一个以第一个字母为键的NSDictionary,其中包含与每个字母关联的对象数组。
假设您有一个对象
@interface CGLContact : NSObject
@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;
@property (nonatomic, readonly) NSString *fullName;
@end
以及一个此类对象的数组
CGLContact *steve = [CGLContact contactWithFirstName:@"Steve" lastName:@"Jobs"];
CGLContact *bill = [CGLContact contactWithFirstName:@"Bill" lastName:@"Gates"];
CGLContact *larry = [CGLContact contactWithFirstName:@"Larry" lastName:@"Ellison"];
CGLContact *eric = [CGLContact contactWithFirstName:@"Eric" lastName:@"Jones"];
NSArray *contacts = @[steve, bill, larry, eric];
您可以按如下方式对它们进行排序
NSDictionary *sortedContacts = [CGLAlphabetizer alphabetizedDictionaryFromObjects:contacts
usingKeyPath:@"lastName"];
// => @{
@"E" : @[larry],
@"G" : @[bill],
@"J" : @[steve, eric]
}
甚至可以按如下方式
NSDictionary *sortedContacts = [CGLAlphabetizer alphabetizedDictionaryFromObjects:contacts
usingKeyPath:@"firstName"];
// => @{
@"B" : @[bill],
@"E" : @[eric],
@"L" : @[larry],
@"S" : @[steve]
}
如果您打算将它们用于UITableView,还有一些方便的方法可供使用
NSArray *indexTitle = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:sortedContacts];
// => @[@"B", @"E", @"L", @"S"]
将这些组件组合起来,对UITableView的内容进行排序可能看起来如下
static NSString * const CGLContactsCellIdentifier = @"CGLContactsCellIdentifier";
@interface CGLContactsTableViewController ()
@property (nonatomic) NSDictionary *alphabetizedDictionary;
@property (nonatomic) NSArray *sectionIndexTitles;
@end
@implementation CGLContactsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CGLContactsCellIdentifier];
}
- (void)setContacts:(NSArray *)contacts {
_contacts = contacts;
self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:_contacts usingKeyPath:@"lastName"];
self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];
[self.tableView reloadData];
}
- (CGLContact *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}
#pragma mark - Table view data source
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.sectionIndexTitles;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.sectionIndexTitles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *sectionIndexTitle = self.sectionIndexTitles[section];
return [self.alphabetizedDictionary[sectionIndexTitle] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CGLContactsCellIdentifier forIndexPath:indexPath];
CGLContact *contact = [self objectAtIndexPath:indexPath];
cell.textLabel.text = contact.fullName;
return cell;
}
@end