美文网首页OC开发iOS Developer
本地搜索--SearchBar--干货

本地搜索--SearchBar--干货

作者: JonesCxy | 来源:发表于2017-02-28 17:48 被阅读46次

    为了节省大家的时间直接上gif

    demo
    工程框架是用sb部署的,所以有些细节没处理好.
    我把自己封装的工具类发上来.
    整个demo我已经传到GitHub上了,有需要的请下载,可以star┗|`O′|┛ 嗷~~
    传送门 https://github.com/JonesCxy/search
    #import "NDSearchTool.h"
    
    @implementation NDSearchTool
    
    
    +(NDSearchTool *)tool{
    
        NDSearchTool *tool = [[NDSearchTool alloc]init];
        
        return tool;
    
    }
    
    
    - (NSArray *)searchWithFieldArray:(NSArray *)fieldArray
                          inputString:(NSString *)inputString
                              inArray:(NSArray *)array
    {
        if (![array count] || ![fieldArray count]) {
            return nil;
        }
        
        NSPredicate *scopePredicate;
        NSMutableArray *backArray = [NSMutableArray array];
        
        for (NSString *fieldString in fieldArray) {
            
            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.%@ contains[c] %@", fieldString, inputString];
            NSArray *tempArray = [array filteredArrayUsingPredicate:scopePredicate];
        
            for (NSObject *object in tempArray) {
                if (![backArray containsObject:object]) {
                    [backArray addObject:object];
                }
            }
        }
        
        return backArray;
    }
    
    
    -(NSArray *)searchWithFieldArray:(NSArray *)allFieldArray
                         inputString:(NSString *)inputString
                          inAllArray:(NSArray *)allArray{
    
    
        NSInteger count = allArray.count;
        if (allFieldArray.count != allArray.count || 0 == count) {
            
            return nil;
        }
        
        NSMutableArray *backArray = [NSMutableArray array];
        
        
        for (NSInteger i = 0; i < count; i++) {
            
            NSArray *tempArray = [self searchWithFieldArray:allFieldArray[i] inputString:inputString inArray:allArray[i]];
            
            [backArray addObject:tempArray];
        }
    
        return backArray;
    }
    
    
    

    相关文章

      网友评论

        本文标题:本地搜索--SearchBar--干货

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