美文网首页
源生方法实现检索与检索数据的排序筛选

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

作者: 为何与何用 | 来源:发表于2018-04-09 13:17 被阅读0次

检索

首先我们先要了解如何实现检索,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;
}

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

结语

· 纯自己手动实现。

相关文章

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

    检索 首先我们先要了解如何实现检索,OC有源生的检索API,使用起来也相当的简单方便。只要实现UITableVie...

  • ElasticSearch全文检索引擎-介绍

    全文检索 一种将文件种或者数据库中所有文本与检索项匹配的文字资料检索方法。对全文数据的检索 数据分类: 结构化数据...

  • 商品检索

    ElasticSearch实现商品检索 商品检索 检索入口 复杂条件检索 检索业务需要考虑的问题 商品基本的数据模...

  • 2018-11-06文献检索与利用作业

    人工智能 检索词:人工智能 数据库:中国学术期刊全文数据库 检索步骤和过滤筛选分析过程 检索:人工智能,检索到95...

  • 3. 排序检索数据

    排序检索数据 排序数据 关系数据库设计理论认为,若不规定排序顺序,则不应该假定检索出的数据的顺序有任何意义 用OR...

  • 《SQL必知必会》—— 检索1

    普通检索 排序检索 过滤数据 通配符过滤(只能用于文本字段(字符串)) 计算字段 函数 汇总数据(不需要检索实际数...

  • 排序检索数据

    字句(Clause),SQL语句都是由若干的子句组成的,这些子句有些是必需的,有些是可选的。子句是由关键字和所提供...

  • 排序检索数据

    order by 默认排序是升序,,但是可以降序使用desc select prod_name from prod...

  • 检索模型与检索排序

    1. 用户查询与返回结果相关度度量 1.1 相似性计算:空间向量余弦夹角 https://www.jianshu....

  • SQL 对检索结果进行排序

    ORDER BY 子句 有如下的顾客信息表: 检索出所有的客户名称: 检索结果: 检索结果未进行过排序的,数据可能...

网友评论

      本文标题:源生方法实现检索与检索数据的排序筛选

      本文链接:https://www.haomeiwen.com/subject/lflphftx.html