开发者

How do I get TTTableViewDataSource to work with AddressBook

I would like to get a TTPickerTextField to search and get data from the build in AddressBook, and I understand I should make my own data source class that implements the TTTableViewDataSource protocol. But how do I implement it so that it connects correctly with the build in A开发者_开发技巧ddressBook? Im a newbie, so combining the Address Book Programming Guide for iOS and the API for TTTableViewDataSource is very confusing to me, so please help with some hints or even example(s).

Thank you


Here is an ABDataSource, that does exactly this. I'm using this in one of my apps. It will not work out of the box, because it depends on my 'DSMEmailAddress' class, but I hope it will point you in the right direction.

You may use this code under the terms of the zlib licence.

@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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜