zoukankan      html  css  js  c++  java
  • UILocalizedIndexedCollation -- 本地化下按首字母排序

    参考:http://nshipster.cn/uilocalizedindexedcollation/

    UILocalizedIndexedCollation 是一个帮助我们组织列表数据的类,它能够根据地区来生成与之对应区域索引标题。不需要直接创建它的对象,我们可以通过 UILocalizedIndexedCollation +currentCollation 获得一个对应当前地区的单例对象。

    UILocalizedIndexedCollation 的首要任务就是决定对于当前地区区域索引标题应该是什么,我们可以通过 sectionIndexTitles 属性来获得它们。

    有了这些区域标题,下一步就是判断每个模型对象分别对应哪个区域了。这可以通过实现 -sectionForObject:collationStringSelector: 做到。这个方法返回 NSInteger 类型的索引,它对应了模型对象的指定方法的返回值。方法名称可以为 localizedNametitle 甚至 description 等。

    显而易见,列表数据源中会有一个数组,它对应了列表中有多少区域,数组元素表示区域中的每一行。由于整理工作是由 UILocalizedIndexedCollation 来做的,因此理所当然地,也应该由它来为每个区域中的行进行排序。和 -sectionForObject:collationStringSelector: 的实现方式类似,– sortedArrayFromArray:collationStringSelector: 可以为我们基于模型对象的本地化标题来排列模型对象。

    - (void)genDataWithCount:(NSInteger)count {
        NSArray *xings = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];
        NSArray *ming1 = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];
        NSArray *ming2 = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""];
        
        for (int i = 0; i < count; i++) {
            NSString *name = xings[arc4random_uniform((int)xings.count)];
            NSString *ming = ming1[arc4random_uniform((int)ming1.count)];
            name = [name stringByAppendingString:ming];
            if (arc4random_uniform(10) > 3) {
                NSString *ming = ming2[arc4random_uniform((int)ming2.count)];
                name = [name stringByAppendingString:ming];
            }
            LXFriends *friend = [LXFriends new];
            friend.name = name;
            [self.dataArray addObject:friend];
        }
        
        [self setupTableSections];
    }
    
    - (void)setupTableSections {
        UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
        
        // sectionArray创建
        NSUInteger numberOfSections = [[collation sectionTitles] count];
        NSMutableArray *newSectionsArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < numberOfSections; i ++) {
            [newSectionsArray addObject:[[NSMutableArray alloc] init]];
        }
        
        // 将数据插入到newSectionArray中
        for (LXFriends *frind in self.dataArray) {
            NSUInteger sectionIndex = [collation sectionForObject:frind collationStringSelector:@selector(name)];
            [newSectionsArray[sectionIndex] addObject:frind];
        }
        
        // 对每一个section中的数据排序
        for (int i = 0; i < numberOfSections; i ++) {
            NSMutableArray *friendsForSection = newSectionsArray[i];
            NSArray *sortedFriendsForSection = [collation sortedArrayFromArray:friendsForSection collationStringSelector:@selector(name)];
            newSectionsArray[i] = sortedFriendsForSection;
        }
        
        // 移除section中没有数据的section
        NSMutableArray *temp = [[NSMutableArray alloc] init];
        [newSectionsArray enumerateObjectsUsingBlock:^(NSArray *array, NSUInteger idx, BOOL * _Nonnull stop) {
            if (array.count == 0) {
                [temp addObject:array];
            }
        }];
        [newSectionsArray removeObjectsInArray:temp];
    }
  • 相关阅读:
    HDU 1042 N!
    linux上安装wps办公软件
    《UNIX环境高级编程》笔记--环境变量
    【StatLearn】统计学习中knn算法实验(2)
    Asp.Net验证控件浅析
    斜率小于0的连线数量-归并排序
    Printk与sched_clock_init的一点分析
    【数字图像处理】使用kmeans算法对TrueColor图片进行优化
    将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
    python中os/sys/platform模块区别
  • 原文地址:https://www.cnblogs.com/muzijie/p/7299078.html
Copyright © 2011-2022 走看看