国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

IOS中獲取本地通訊錄聯(lián)系人以及漢字首字母排序

2019-10-21 18:49:44
字體:
供稿:網(wǎng)友

iOS中獲取手機(jī)通訊錄中的聯(lián)系人信息:

/*** 加載本地聯(lián)系人*/ - (void)loadLocalContacts {   //新建一個(gè)通訊錄類   ABAddressBookRef addressBooks = nil;      if (DeviceVersion < 6.0) {     addressBooks = ABAddressBookCreate();   } else {     addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);     //獲取通訊錄權(quán)限     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);   }      //判斷授權(quán)狀態(tài)   if (ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized) {     return ;   }      //獲取通訊錄中的所有人   CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);   //通訊錄中人數(shù)   CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);   NSMutableArray *persons = [[NSMutableArray alloc] init];   for (int i = 0; i < nPeople; i++) {     //獲取個(gè)人     ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);     //獲取個(gè)人名字     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];     }   }      //對(duì)聯(lián)系人進(jìn)行分組和排序   UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];   NSInteger highSection = [[theCollation sectionTitles] count]; //中文環(huán)境下返回的應(yīng)該是27,是a-z和#,其他語言則不同      //_indexArray 是右側(cè)索引的數(shù)組,也是secitonHeader的標(biāo)題   _indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]];      NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection];   //初始化27個(gè)空數(shù)組加入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個(gè)數(shù)組中去     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 是聯(lián)系人數(shù)組(確切的說是二維數(shù)組)   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; }  //索引列點(diǎn)擊事件 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {   return index; } 

還有兩個(gè)很重要的方法:

下面這個(gè)方法是[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 是這里的p對(duì)象要實(shí)現(xiàn)的方法,我這里的p是NSString,你也可以用其他對(duì)象例如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; } 

下面這個(gè)方法是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; } 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥棱县| 靖江市| 渝北区| 百色市| 平顶山市| 女性| 泽库县| 区。| 黄石市| 黄平县| 专栏| 仙游县| 株洲市| 临沂市| 南江县| 城步| 定兴县| 平利县| 左贡县| 奉节县| 定南县| 锦州市| 北川| 民丰县| 东乌珠穆沁旗| 沾化县| 砀山县| 休宁县| 时尚| 陇西县| 华亭县| 安义县| 边坝县| 武乡县| 武平县| 绵阳市| 新化县| 新河县| 五台县| 峨边| 潼南县|