源生方法实现检索与检索数据的排序筛选

检索

首先我们先要了解如何实现检索,OC有源生的检索API,使用起来也相当的简单方便。只要实现UITableView的两个代理方法即可:

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

这个方法返回检索字母数组,我们看看官方文档:


image

大致意思就是让你返回一个字符数组,右侧检索条会显示你这个数组中的字符。

还有一个方法:

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

这个方法是一个事件回调,返回你点击检索栏的字符和其在数组中的下标。

如果要展示列表字母头:

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

当然UITableView要设置为group模式。

如果要修改检索栏颜色:
tableView.sectionIndexColor = [UIColor blueColor];

效果图
image

检索数据的筛选排序

当然,很可能我们从后台拿到的数据是无序的,那么就要我们客户端这里做筛选和排序了。

这里我还是用OC源生的API接口来实现:UILocalizedIndexedCollation。
这里需要的是一个二维数组,所以就自制的一个,并做了个简单的封装,废话不多说,直接上代码:

#pragma mark - 数据分类、排序,array:模型数组
- (void)classAndCompareWithArray:(NSMutableArray *)array{
    
    UILocalizedIndexedCollation *localizedCollection = [UILocalizedIndexedCollation currentCollation];
    
    //得出collation索引的数量,这里是27个(26个字母和1个#)
    NSInteger sectionTitlesCount = [localizedCollection sectionTitles].count;
    
    //初始化一个数组newSectionsArray用来存放最终的数据
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    //初始化27个空数组加入newSectionsArray
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *arrM = [NSMutableArray array];
        [newSectionsArray addObject:arrM];
    }
    
    //将每个地区按name分到某个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个字符串
    NSMutableArray *charactorTitleArrM = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
    
    //对每个section中的数组按照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数组的下标所对应的数据
        if (sortedSectionArr.count == 0 || sortedSectionArr == nil){
            
//            NSLog(@"aaa--%d",index);
            [emptyCharactorArrM addObject:charactorTitleArrM[index]];
            [emptySectionArrM addObject:newSectionsArray[index]];
        }
    }
    
    //删除空的数组
//    for (NSString *charactor in emptyCharactorArrM) {
//        //检索不展示没有数据的字母
//        [charactorTitleArrM removeObject:charactor];
//    }
//    for (NSArray *sectionArr in emptySectionArrM) {
//        
//        [newSectionsArray removeObject:sectionArr];
//    }
    
//    //删除空数组
//    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;
    //最终二维数组
    self.joinScetionArr = newSectionsArray.copy;
}

如果不需要显示空的数组和检索字符,就打开注释部分。

结语

· 纯自己手动实现。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 体彩338期毒胆7次2 复式23457 形态。。。组六。。。防组三 和差019 单挑 527 427 327 33...
    冰风_fb43阅读 106评论 0 1
  • 我叫慕秋凉,我喜欢的少年,他叫徐向阳。 他说,我的名字很好听。 ...
    辞十二阅读 279评论 0 1