源生方法實(shí)現(xiàn)檢索與檢索數(shù)據(jù)的排序篩選

檢索

首先我們先要了解如何實(shí)現(xiàn)檢索,OC有源生的檢索API,使用起來(lái)也相當(dāng)?shù)暮?jiǎn)單方便。只要實(shí)現(xiàn)UITableView的兩個(gè)代理方法即可:

- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

這個(gè)方法返回檢索字母數(shù)組,我們看看官方文檔:


image

大致意思就是讓你返回一個(gè)字符數(shù)組,右側(cè)檢索條會(huì)顯示你這個(gè)數(shù)組中的字符。

還有一個(gè)方法:

- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
               atIndex:(NSInteger)index;

這個(gè)方法是一個(gè)事件回調(diào),返回你點(diǎn)擊檢索欄的字符和其在數(shù)組中的下標(biāo)。

如果要展示列表字母頭:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

當(dāng)然UITableView要設(shè)置為group模式。

如果要修改檢索欄顏色:
tableView.sectionIndexColor = [UIColor blueColor];

效果圖
image

檢索數(shù)據(jù)的篩選排序

當(dāng)然,很可能我們從后臺(tái)拿到的數(shù)據(jù)是無(wú)序的,那么就要我們客戶端這里做篩選和排序了。

這里我還是用OC源生的API接口來(lái)實(shí)現(xiàn):UILocalizedIndexedCollation。
這里需要的是一個(gè)二維數(shù)組,所以就自制的一個(gè),并做了個(gè)簡(jiǎn)單的封裝,廢話不多說(shuō),直接上代碼:

#pragma mark - 數(shù)據(jù)分類、排序,array:模型數(shù)組
- (void)classAndCompareWithArray:(NSMutableArray *)array{
    
    UILocalizedIndexedCollation *localizedCollection = [UILocalizedIndexedCollation currentCollation];
    
    //得出collation索引的數(shù)量,這里是27個(gè)(26個(gè)字母和1個(gè)#)
    NSInteger sectionTitlesCount = [localizedCollection sectionTitles].count;
    
    //初始化一個(gè)數(shù)組newSectionsArray用來(lái)存放最終的數(shù)據(jù)
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    //初始化27個(gè)空數(shù)組加入newSectionsArray
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *arrM = [NSMutableArray array];
        [newSectionsArray addObject:arrM];
    }
    
    //將每個(gè)地區(qū)按name分到某個(gè)section下
    for (CircleListModel *model in array) {
        //獲取name屬性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就為11
        NSInteger sectionNumber = [localizedCollection sectionForObject:model collationStringSelector:@selector(name)];
        NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
        [sectionNames addObject:model];
    }
    
    //獲取A - # 27個(gè)字符串
    NSMutableArray *charactorTitleArrM = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
    
    //對(duì)每個(gè)section中的數(shù)組按照name屬性排序
    NSMutableArray *emptyCharactorArrM = [NSMutableArray array];
    NSMutableArray *emptySectionArrM = [NSMutableArray array];
    for (int index = 0; index < sectionTitlesCount; index++) {
        
        NSMutableArray *sectionArrM = newSectionsArray[index];
        
        NSArray *sortedSectionArr = [localizedCollection sortedArrayFromArray:sectionArrM collationStringSelector:@selector(name)];
        
        newSectionsArray[index] = sortedSectionArr;
        
        //記錄空de數(shù)組的下標(biāo)所對(duì)應(yīng)的數(shù)據(jù)
        if (sortedSectionArr.count == 0 || sortedSectionArr == nil){
            
//            NSLog(@"aaa--%d",index);
            [emptyCharactorArrM addObject:charactorTitleArrM[index]];
            [emptySectionArrM addObject:newSectionsArray[index]];
        }
    }
    
    //刪除空的數(shù)組
//    for (NSString *charactor in emptyCharactorArrM) {
//        //檢索不展示沒(méi)有數(shù)據(jù)的字母
//        [charactorTitleArrM removeObject:charactor];
//    }
//    for (NSArray *sectionArr in emptySectionArrM) {
//        
//        [newSectionsArray removeObject:sectionArr];
//    }
    
//    //刪除空數(shù)組
//    for (int i=0; i<newSectionsArray.count; i++) {
//
//        if ([newSectionsArray[i] isKindOfClass:[NSMutableArray class]]){
//
//            NSLog(@"a---%d",i);
//            NSMutableArray *arrM = newSectionsArray[I];
//            if (arrM.count == 0){
//
//                NSLog(@"a-a-a-%d",i);
//                [charactorTitleArrM removeObjectAtIndex:i];
//                [newSectionsArray removeObject:arrM];
//            }
//        }else if ([newSectionsArray[i] isKindOfClass:[NSArray class]]){
//
//            NSLog(@"b---%d",i);
//            NSArray *arr = newSectionsArray[I];
//            if (arr.count == 0){
//
//                NSLog(@"b-b-b-%d",i);
//                [charactorTitleArrM removeObjectAtIndex:i];
//                [newSectionsArray removeObject:arr];
//            }
//        }else{
//
//            NSLog(@"c---%d",i);
//            [charactorTitleArrM removeObjectAtIndex:i];
//            [newSectionsArray removeObjectAtIndex:i];
//        }
//
//    }
    
    //檢索欄
    self.charactorTitleArr = charactorTitleArrM.copy;
    //最終二維數(shù)組
    self.joinScetionArr = newSectionsArray.copy;
}

如果不需要顯示空的數(shù)組和檢索字符,就打開(kāi)注釋部分。

結(jié)語(yǔ)

· 純自己手動(dòng)實(shí)現(xiàn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 體彩338期毒膽7次2 復(fù)式23457 形態(tài)。。。組六。。。防組三 和差019 單挑 527 427 327 33...
    冰風(fēng)_fb43閱讀 126評(píng)論 0 1
  • 我叫慕秋涼,我喜歡的少年,他叫徐向陽(yáng)。 他說(shuō),我的名字很好聽(tīng)。 ...
    辭十二閱讀 296評(píng)論 0 1

友情鏈接更多精彩內(nèi)容