Localization under the first letter sorting artifact--uilocalizedindexedcollation

Source: Internet
Author: User

Recently in the whole very simple address book related projects, of course, it is not the first letter or alphabetical alphabetical grouping index. Because in accordance with my usual practice, are want to make more general, support localization, so this is tangled, the world language ah I go, I at most also know Chinese and English, which can not be used before those such as the Chinese character into pinyin re-ordering method, not high efficiency does not say, the localization of other countries is not feasible. An accidental opportunity, I found that the SDK has provided a implementation of this feature artifact--uilocalizedindexedcollation.

First of all, the uilocalizedindexedcollation sorting is based on the operation of the object. Below I will give a chestnut to explain. There is a person class known first:

1 @interface Person:nsobject 2 @property (nonatomic, Strong) NSString *name; 3 @end

Then initialize some objects into an array ( Note: For subsequent instructions convenient, I directly take the value of name to represent the object of the person class, the actual encoding is to use the Object!)! such as the following < Lindane > represents p.name = @ "lindane" for the Person class object P)

1 nsarray *srcarray = @[< linrong >, < lindane >, < Jay >, < shuren >, < Jay >, < hwa >];

Initialize the uilocalizedindexedcollation first,

1 uilocalizedindexedcollation *collation = [Uilocalizedindexedcollation currentcollation];

It can be seen that this is a singleton, which initializes different results based on the language of the different countries. such as Chinese and English to get is a~z and #, Japanese is A~z,あ,か,さ,た,な,は,ま,や,ら,わ and #. Below I take the most familiar Chinese environment as an example, directly on the code, pay attention to the comment section of the explanation

1 //Get the number of collation indexes, here are 27 (26 letters and one #)2Nsinteger Sectiontitlescount =[[Collation Sectiontitles] count];3 4 //initialize an array newsectionsarray to hold the final data, and the final data model we want to get should be shaped like an array of data that begins with a @[@[of a, @[an array of data starting with B], @[an array of data that begins with C], ... @[to # (others) Array of data beginning with]]5 6Nsmutablearray *newsectionsarray =[[Nsmutablearray alloc] initwithcapacity:sectiontitlescount];7     8 //initializes 27 empty arrays to join Newsectionsarray9  for(Nsinteger index =0; Index < Sectiontitlescount; index++) {TenNsmutablearray *array =[[Nsmutablearray alloc] init]; One [Newsectionsarray Addobject:array]; A } -      - //Place everyone under a section by name the  -  for(Person *pinchSrcarray) { -     //gets the location of the value of the Name property, such as "Lindane", the first letter is L, row 11th in A~z (the first digit is 0), and the Sectionnumber is -Nsinteger Sectionnumber =[Collation Sectionforobject:p collationstringselector: @selector (name)]; +     //add a P named "Lindane" to the 11th array in Newsectionsarray. -Nsmutablearray *sectionnames =Newsectionsarray[sectionnumber]; + [Sectionnames addobject:p]; A } at      - //sort the array in each section by the Name property -  for(Nsintger index =0; Index < Sectiontitlescount; index++) { -Nsmutablearray *personarrayforsection =Newsectionsarray[index]; -Nsarray *sortedpersonarrayforsection =[Collation Sortedarrayfromarray:personarrayforsection collationstringselector: @selector (name)]; -Newsectionsarray[index] =sortedpersonarrayforsection; in}

Finally the newsectionsarray should be shaped like @[@[< hwa;], @[], @[], ... @[< lindane;, < Linrong;], ... @[], @[< Jay;, < Jay Chou; < Shuren;]]

The follow-up work is to associate this array as a data source with UITableView through the TableView delegate, some of which are not described here

1-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section {2     return[Collation sectiontitles][section];3 }4 5-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView {6     return[Collation Sectionindextitles];7 }8 9-(Nsinteger) TableView: (UITableView *) TableView sectionforsectionindextitle: (NSString *) title Atindex: (Nsinteger) Index {Ten     return[Collation Sectionforsectionindextitleatindex:index]; One}

However, the use of this uilocalizedindexedcollation has a disadvantage is not to distinguish between the surname of the Polyphone, such as "Zeng" will be divided into the "C" group, do not know if you have a good method based on this reply below. Below is Apple's official example, where the 3rd one is about uilocalizedindexedcollation and can be downloaded to learn about http://developer.apple.com/library/ios/samplecode/ Tableviewsuite/introduction/intro.html

Localization under the first letter sorting artifact--uilocalizedindexedcollation

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.