关闭

20160830UILocalizedIndexedCollation的使用

标签: 排序UILocalizedIndexedCollation
235人阅读 评论(0) 收藏 举报
分类:

我在开发中曾经有这样的需求,就是对通讯录的字符串排序,这个需求很大众,我参与开发的两个app都遇到,我是一般会追求最优解决方案的,发现了UILocalIndexedCollation,那比自己写得排序要强大多了吧,如果是自己的排序要考虑很多问题的,比如多音字问题,有时排序不怎么正确,尤其是对于一些特殊问题,而且效率不高,对于很大数据量,所以还是选择UILocalIndexedCollation比较合适。 本文主要介绍 UILocalIndexedCollation 的使用。查看苹果文档

The UILocalizedIndexedCollation class is a convenience for organizing, sorting, and localizing the data for a table view that has a section index. The table view’s data source then uses the collation object to provide the table view with input for section titles and section index titles.

Table views with section indexes are ideal for displaying and facilitating the access of data composed of many items organized by a sequential ordering scheme such as the alphabet. Users tap an index title to jump to the corresponding section. The initial table view of the Phone/Contacts application on the iPhone is an example. Note that the section titles can be different than the titles of the index. 

To prepare the data for a section index, your table-view controller creates a indexed-collation object and then, for each model object that is to be indexed, calls sectionForObject:collationStringSelector:. This method determines the section in which each of these objects should appear and returns an integer that identifies the section. The table-view controller then puts each object in a local array for its section. For each section array, the controller calls the sortedArrayFromArray:collationStringSelector: method to sort all of the objects in the section. The indexed-collation object is now the data store that the table-view controller uses to provide section-index data to the table view, as illustrated in Listing 1.

上面第三段文字给出了大概的解题思路,我们首先可以将需要排序的数据分成几个section 部分,在对每个部分进行排序。下面来段实例代码。


    UILocalizedIndexedCollation *indexedCollation =
    [UILocalizedIndexedCollation currentCollation];
    
    //  Iterate over the products, populating their section number
    for (Product *theProduct in productsTemp) {
        NSInteger section = [indexedCollation sectionForObject:theProduct
                                       collationStringSelector:@selector(name)];
        theProduct.section = section;
    }
    
    //  Get the count of the number of sections
    NSInteger sectionCount = [[indexedCollation sectionTitles] count];
    
    //  Create an array to hold the sub arrays
    NSMutableArray *sectionsArray = [NSMutableArray
                                     arrayWithCapacity:sectionCount];
    
    // Iterate over each section, creating each sub array
    for (int i=0; i<=sectionCount; i++) {
        NSMutableArray *singleSectionArray = [NSMutableArray
                                              arrayWithCapacity:1];
        [sectionsArray addObject:singleSectionArray];
    }
    
    // Iterate over the products putting each product into the correct sub-array
    for (Product *theProduct in productsTemp) {
        [(NSMutableArray *)[sectionsArray objectAtIndex:theProduct.section]
         addObject:theProduct];
    }
    
    // Iterate over each section array to sort the items in the section
    for (NSMutableArray *singleSectionArray in sectionsArray) {
        // Use the UILocalizedIndexedCollation sortedArrayFromArray: method to
        // sort each array
        NSArray *sortedSection = [indexedCollation
                                  sortedArrayFromArray:singleSectionArray
                                  
                                  collationStringSelector:@selector(name)];
        [self.products addObject:sortedSection];
    }

productsTemp是需要排序的数据,Pruduct 是一条数据的模型,product可以有 name ,manufacture,details等字段,现在可以添加section这个字段,通过 

sectionForObject:collationStringSelector: 获得这个section并且保存。获取每部分的 sectionArray ,然后对没部分里面的数据进行排序,可以通过

sortedArrayFromArray:collationStringSelector

这个函数,最后得到 sortedSection 排好序的数据。




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12744次
    • 积分:449
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论