UILocalizedIndexedCollation的使用

UILocalizedIndexedCollation 是一個幫助咱們組織列表數據的類,它可以根據地區來生成與之對應區域索引標題。不須要直接建立它的對象,咱們能夠經過 UILocalizedIndexedCollation +currentCollation 得到一個對應當前地區的單例對象。數組

下表能夠幫助你瞭解不一樣地區區域索引標題的差異。若是你想要看這些的話,你須要把對應的地區加入到你的項目本地化列表中。app

Locale Section Index Titles
en_US A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, #
ja_JP A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, あ, か, さ, た, な, は, ま, や, ら, わ, #
sv_SE A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, Å, Ä, Ö, #
ko_KO A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, ㄱ, ㄴ, ㄷ, ㄹ, ㅁ, ㅂ, ㅅ, ㅇ, ㅈ, ㅊ, ㅋ, ㅌ, ㅍ, ㅎ, #
+ (instancetype)currentCollation;

// 分組結果的標題列表 (例 A-Z,# in US/English)
@property(nonatomic, readonly) NSArray<NSString *> * sectionTitles;
@property(nonatomic, readonly) NSArray<NSString *> *sectionIndexTitles;
// Specifies the section that should be scrolled to for the title at the given index.
// This method allows you to map between a given item in the index
// and a given section where there isn't a one-to-one mapping.
- (NSInteger)sectionForSectionIndexTitleAtIndex:(NSInteger)indexTitleIndex;

//返回包含該對象的索引位置
// selector 必須返回一個NSString.
- (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;

// 對一個數字進行快速排序,一樣selector 必須返回一個NSString
- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;

 

對數組中TestModel按照首字母進行排序,以下:測試

第一種方法:沒有建立selector方法,直接採用的TestModel 的 testName的get方法,進行排序,可是排序後結果是【#,A-Z】。但我想要的是【A-Z, #】的順序atom

建立第二種方法:建立testSelector方法,對testName進行處理後返回lua

若是沒有特殊要求,能夠直接用tetsModel的get方法,- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector 中的selector能夠是任何返回NSString參數的方法,要是有特殊需求能夠自定義selector。spa

@interface TestModel : NSObject
@property(nonatomic, copy) NSString *testname;
@end

@implementation TestModel
- (NSString *)testSelector {
    NSString *regex = @"^[\u4e00-\u9fa5A-Za-z].*$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isValid = [predicate evaluateWithObject:self.testname];
    if (isValid) {
        return self.testname;
    }
    return [NSString stringWithFormat:@"ZZZZZZZZ%@", self.testname];
}
@end
NSMutableArray *mut = [NSMutableArray array];
    TestModel *model1 = [TestModel new];
    model1.testname = @"@駕照";
    [mut addObject:model1];
    
    TestModel *model2 = [TestModel new];
    model2.testname = @"123測試";
    [mut addObject:model2];
    
    TestModel *model3 = [TestModel new];
    model3.testname = @"A汽車";
    [mut addObject:model3];
    
    TestModel *model4 = [TestModel new];
    model4.testname = @"快速";
    [mut addObject:model4];
    
    TestModel *model5 = [TestModel new];
    model5.testname = @"軟件";
    [mut addObject:model5];
    
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    NSArray *sortMut = [collation sortedArrayFromArray:mut collationStringSelector:@selector(testname)];
    for (TestModel *p in sortMut) {
        NSLog(@"%@", p.testname);
    }
    /*
     @駕照
     123測試
     A汽車
     快速
     軟件
     */
    
    NSArray *customSort = [collation sortedArrayFromArray:mut collationStringSelector:@selector(testSelector)];
    for (TestModel *p in customSort) {
        NSLog(@"%@", p.testname);
    }
    /*
     A汽車
     快速
     軟件
     @駕照
     123測試
     */
相關文章
相關標籤/搜索