IOS implements similar contacts sorted by pinyin-PINYIN4OBJC

Source: Internet
Author: User

I use PinYin4Objc this library to implement this feature in projects that require the ability to sort by pinyin and the display of indexed lists like contacts.

PinYinObjcis a very efficient Chinese pinyin library, wisdom teeth simplified and Traditional Chinese, has the following features:

1. High efficiency, using data cache, after the first initialization, pinyin data into the file cache and memory cache, the conversion efficiency is greatly improved; 2. Support custom formatting, pinyin case, etc. 3. Pinyin data integrity, support Chinese Simplified and traditional, and the network of popular related projects, data is full, There is almost no problem with conversion errors.

Download PINYINOBJC

Actual application project requirements in the project:

Show a list of members of a class, one is the administrator to rank at the top, following the alphabetical list to implement the index, as follows:

Code implementation process

Querying the database for a list of members

1 // the list of members is sorted according to the ISAdmin field--order by ISAdmin 2 Nsmutablearray *members = [[Asmemberdao sharedinstance] queryallmembersbygroupid:groupid]; 3 // based on sorted query results The first one for the administrator 4 ascontact *memeberadmin = [Members Objectatindex:0];
View Code

Convert each member's name to Pinyin

1 //initializes the Hanyupinyinoutputformat object, setting the appropriate type2Hanyupinyinoutputformat *outputformat=[[Hanyupinyinoutputformat alloc] init];3 [OutputFormat Settonetype:tonetypewithouttone];4 [OutputFormat SETVCHARTYPE:VCHARTYPEWITHV];5 [OutputFormat setcasetype:casetypeuppercase];6 //iterate through the list of members, turn member names contactName into Pinyin, and store them in the CategoryName field for sorting7[Members enumerateobjectsusingblock:^ (IDobj, Nsuinteger idx, BOOL *stop) {8Ascontact *contact = (Ascontact *) obj;9NSString *outputpinyin=[pinyinhelper ToHanyuPinyinStringWithNSString:contact.contactName Withhanyupinyinoutputformat:outputformat withnsstring:@""];TenContact.categoryname =[Outputpinyin uppercasestring]; One }]; A[OutputFormat release];
View Code

Sort the list of members according to the phonetic field CategoryName

1 nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] Initwithkey:@ "CategoryName " Ascending:yes]; 2 Nsarray *array = [[Nsarray alloc] initwithobjects:sortdescriptor, nil]; 3 [Members Sortusingdescriptors:array]; 4 [Array release]; 5 [Sortdescriptor release];
View Code

Define a global variable datadictionary to organize data structures

key: 将汉字转完拼音后的第一个字母, 也就是 section 中的 A、B、C...

value: 是一个成员数组,存放每个 section 下的成员列表

For example, A is a Key to the dictionary, and the corresponding value is the member array {AF1, af10}, and of course the member object is stored in the array.

1Datadictionary =[[Nsmutabledictionary alloc] init];2 //storing the array of members under each section3Nsmutablearray *currentarray =Nil;4 //used to get the first letter in Pinyin5Nsrange Arange = Nsmakerange (0,1);6NSString *firstletter =Nil;7 //Traverse Member list organization data structure8  for(Ascontact *contactinchMembers ) {9     //If it is an administrator, it is temporarily not placed in the DatadictionaryTen     if(Contact.isadmin = =1) { One          Continue; A     } -     //gets the first letter in the phonetic Alphabet, joins the member directly into the current member array if it already exists, creates the member data if it does not exist, adds a key-value structure to the Datadictionary -Firstletter =[Contact.categoryname Substringwithrange:arange]; the     if([datadictionary objectforkey:firstletter] = =Nil) { -CurrentArray =[Nsmutablearray array]; - [Datadictionary Setobject:currentarray forkey:firstletter]; -     } + [CurrentArray addobject:contact]; -}
View Code

Defines a global variable allKeys used to display indexes in the index list

1 //remove key from datadictionary and sort2AllKeys =[[Nsmutablearray alloc] initwitharray:[[datadictionary AllKeys] Sortedarrayusingfunction:sortobjectsbykey Context:null]];3 //then add the administrator to the front of the ordered AllKeys4[AllKeys InsertObject:@"Administrator"Atindex:0];5[Datadictionary Setobject:[nsarray arraywithobjects:contactadmin, nil] Forkey:@"Administrator"];
View Code

Finally, through allKeys and dataDictionary configuration of the TableView agent is OK, here is not to repeat

Hope that you will be helpful in reading this article

IOS implements similar contacts sorted by pinyin-PINYIN4OBJC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.