IOSでローカルアドレス帳の連絡先と漢字の頭文字のソートを取得
6746 ワード
iOSで携帯電話の連絡先情報を取得します.
ついでにインデックスとtableView dataSourceの代理方法も貼っておきます.
もう2つの重要な方法があります
以下の方法は[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)];ここのpオブジェクトが実現する方法です.私のここのpはNSStringです.Personなどの他のオブジェクトも使えます.
次の方法はNSStringのカテゴリメソッドです
読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!
/*** */
- (void)loadLocalContacts
{
//
ABAddressBookRef addressBooks = nil;
if (DeviceVersion < 6.0) {
addressBooks = ABAddressBookCreate();
} else {
addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
//
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
//
if (ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized) {
return ;
}
//
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
//
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
NSMutableArray *persons = [[NSMutableArray alloc] init];
for (int i = 0; i < nPeople; i++) {
//
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSMutableString *name = [[NSMutableString alloc] init];
if (firstName == nil && lastName == nil) {
NSLog(@" ");
name = nil;
}
if (lastName) {
[name appendString:lastName];
}
if (firstName) {
[name appendString:firstName];
}
ABMultiValueRef tmlphone = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *telphone = (NSString *)ABMultiValueCopyValueAtIndex(tmlphone, 0);
if (telphone != nil) {
telphone = [telphone stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *title = [NSString stringWithFormat:@"%@(%@)",name,telphone];
[persons addObject:title];
}
}
//
UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];
NSInteger highSection = [[theCollation sectionTitles] count]; // 27, a-z #,
//_indexArray , secitonHeader
_indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection];
// 27 newSectionsArray
for (NSInteger index = 0; index < highSection; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
[array release];
}
for (NSString *p in persons) {
// name , " ", L, A~Z 11( 0),sectionNumber 11
NSInteger sectionNumber = [theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)];
// name “ ” p newSectionsArray 11
NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
[sectionNames addObject:p];
}
for (int i = 0; i < newSectionsArray.count; i++) {
NSMutableArray *sectionNames = newSectionsArray[i];
if (sectionNames.count == 0) {
[newSectionsArray removeObjectAtIndex:i];
[_indexArray removeObjectAtIndex:i];
i--;
}
}
//_contacts ( )
self.contacts = newSectionsArray;
[newSectionsArray release];
[self.tableView reloadData];
}
ついでにインデックスとtableView dataSourceの代理方法も貼っておきます.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.contacts.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.contacts[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"contactCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.imageView.image = [UIImage imageNamed:@"default_head"];
cell.textLabel.text = [self.contacts objectAtIndex:indexPath.section][indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_indexArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _indexArray;
}
//
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
もう2つの重要な方法があります
以下の方法は[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)];ここのpオブジェクトが実現する方法です.私のここのpはNSStringです.Personなどの他のオブジェクトも使えます.
NSString *ret = @"";
if (![self canBeConvertedToEncoding: NSASCIIStringEncoding]) {//
if ([[self letters] length]>2) {
ret = [[self letters] substringToIndex:1];
}
}
else {
ret = [NSString stringWithFormat:@"%c",[self characterAtIndex:0]];
}
return ret;
}
次の方法はNSStringのカテゴリメソッドです
- (NSString *)letters{
NSMutableString *letterString = [NSMutableString string];
int len = [self length];
for (int i = 0;i < len;i++)
{
NSString *oneChar = [[self substringFromIndex:i] substringToIndex:1];
if (![oneChar canBeConvertedToEncoding:NSASCIIStringEncoding]) {
NSArray *temA = makePinYin2([oneChar characterAtIndex:0]);
if ([temA count]>0) {
oneChar = [temA objectAtIndex:0];
}
}
[letterString appendString:oneChar];
}
return letterString;
}
読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!