Skip to content

Instantly share code, notes, and snippets.

@qubblr
Last active August 29, 2015 14:17
Show Gist options
  • Save qubblr/1a50fdbdf0fc8ee29286 to your computer and use it in GitHub Desktop.
Save qubblr/1a50fdbdf0fc8ee29286 to your computer and use it in GitHub Desktop.
Abstract. Loads address book contents. Subclass, implement cellForRow.. and you're good to go. required: pod 'APAddressBook' (https://github.com/Alterplay/APAddressBook)
//
// AddressBookViewController.h
//
// Created by Vladislav Kartashov on 28/03/15.
// Copyright (c) 2015 Vladislav Kartashov. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "APAddressBook.h"
// call reloadContacts in subclass
@interface APAddressBookViewController : UITableViewController
// full access to addressBook. don't be shy to use custom masks and sortDescriptors
@property (strong, nonatomic) APAddressBook *addressBook;
@property (strong, nonatomic) NSArray *contacts; // of ABContact
- (void)reloadContacts;
// override to handle access errors
- (void)didLoadContactsWithError:(NSError *)error;
@end
//
// AddressBookViewController.m
//
// Created by Vladislav Kartashov on 28/03/15.
// Copyright (c) 2015 Vladislav Kartashov. All rights reserved.
//
#import "APAddressBookViewController.h"
#import "APContact.h"
@interface APAddressBookViewController()
@end
@implementation APAddressBookViewController
#pragma mark - Public
- (void)reloadContacts {
[self.addressBook loadContacts:^(NSArray *contacts, NSError *error) {
if (!error) {
self.contacts = contacts;
} else {
[self didLoadContactsWithError:error];
}
}];
}
- (APAddressBook *)addressBook {
if (!_addressBook) {
_addressBook = [[APAddressBook alloc] init];
}
return _addressBook;
}
- (void)didLoadContactsWithError:(NSError *)error {
// handle an error
}
// sort contacts from addressbook into sections to support section titles
- (void)setContacts:(NSArray *)contacts {
SEL selector = @selector(lastName);
NSInteger sectionTitlesCount = [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
NSMutableArray *mutableSections = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
[mutableSections addObject:[NSMutableArray array]];
}
for (id object in contacts) {
NSInteger sectionNumber = [[UILocalizedIndexedCollation currentCollation] sectionForObject:object collationStringSelector:selector];
[[mutableSections objectAtIndex:sectionNumber] addObject:object];
}
for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
NSArray *objectsForSection = [mutableSections objectAtIndex:idx];
// in order to make this work we need to ensure that lastName selector never returns nil.
// currently it's done with hack in ABContact sources. lastName always returns NSString
[mutableSections replaceObjectAtIndex:idx withObject:[[UILocalizedIndexedCollation currentCollation] sortedArrayFromArray:objectsForSection collationStringSelector:selector]];
}
_contacts = mutableSections;
[self.tableView reloadData];
}
#pragma mark - TableViewController DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.contacts.count ?: 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.contacts[section] count] ?: 0;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionTitles];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.addressBook.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES] ];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment