Voici une ABDataSource, qui fait exactement cela. J'utilise ceci dans une de mes applications. Ça ne marchera pas, parce que ça dépend de ma classe 'DSMEmailAddress', mais j'espère que ça va vous pointer dans la bonne direction.
Vous pouvez utiliser ce code selon les termes de la licence zlib.
@interface ABDataSource : TTListDataSource {
ABAddressBookRef _addressBookRef;
NSMutableArray* _allItems;
NSMutableArray* _delegates;
}
+ (ABDataSource*)abDataSource:(BOOL)forSearch;
@end
@implementation ABDataSource
+ (ABDataSource*)abDataSource:(BOOL)forSearch {
ABDataSource* dataSource = [[[ABDataSource alloc] init] autorelease];
return dataSource;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
[_allItems release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// UITableViewDataSource
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView {
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTTableViewDataSource
- (NSString*)tableView:(UITableView*)tableView labelForObject:(id)object {
DSMEmailAdress* field = object;
return field.name;
}
- (Class)tableView:(UITableView*)tableView cellClassForObject:(id)object {
return [DSMEmailAddressTableCell class];
}
- (void)tableView:(UITableView*)tableView prepareCell:(UITableViewCell*)cell
forRowAtIndexPath:(NSIndexPath*)indexPath {
cell.accessoryType = UITableViewCellAccessoryNone;
((TTTableViewCell*)cell).object =[_items objectAtIndex:indexPath.row];
}
- (void)search:(NSString*)text {
if (nil == _allItems) {
_addressBookRef = ABAddressBookCreate();
NSArray* allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);
_allItems = [[NSMutableArray alloc] initWithCapacity:[allPeople count]]; // capacity is only a rough guess, but better than nothing
for (id record in allPeople) {
CFTypeRef emailProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonEmailProperty);
NSArray *emails = (NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
CFRelease(emailProperty);
for (NSString *email in emails) {
NSString* compositeName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
DSMEmailAdress* field = [[[DSMEmailAdress alloc] initWithName:compositeName mail:email] autorelease];
[compositeName release];
[_allItems addObject:field];
}
[emails release];
}
CFRelease(_addressBookRef);
_addressBookRef = nil;
[allPeople release];
allPeople = nil;
}
[_items release];
if (text.length) {
_items = [[NSMutableArray alloc] init];
for (DSMEmailAdress* mail in _allItems) {
if ([mail hasPrefix:text]) {
[_items addObject:mail];
}
}
if ([_items count]==0){
[_items release];
_items = nil;
}
} else {
_items = nil;
}
[_delegates perform:@selector(modelDidFinishLoad:) withObject:self];
}
#pragma mark TTModel
- (NSMutableArray*)delegates {
if (!_delegates) {
_delegates = TTCreateNonRetainingArray();
}
return _delegates;
}
- (BOOL)isLoadingMore {
return NO;
}
- (BOOL)isOutdated {
return NO;
}
- (BOOL)isLoaded {
return !!_allItems;
}
- (BOOL)isLoading {
return NO;
}
- (BOOL)isEmpty {
return !_items.count;
}
- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
}
- (void)invalidate:(BOOL)erase {
}
- (void)cancel {
[_delegates perform:@selector(modelDidCancelLoad:) withObject:self];
}
@end