• iOS中搜索框EVNCustomSearchBar使用小结


      最近在项目开发中用到了搜索框,之前都是用的系统的searchbar,现有项目中用的是EVNCustomSearchBar,我试了一下还挺方便,下面说一下具体的用法。

      第一步:引入添加相关的委托代理EVNCustomSearchBarDelegate懒加载初始化对象

      

    if (!_searchBar) {

            _searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(16, 0, K_CC_SCREEN_WIDTH-32, 44)];

            _searchBar.backgroundColor = K_CC_COLOR_STRING(@"#F5F5F5");

            _searchBar.textColor = K_CC_COLOR_STRING(@"#999999");

            _searchBar.textFieldColor = [UIColor clearColor];

            _searchBar.iconImage = K_CC_IMAGE(@"newhome_search");

            _searchBar.iconAlign = EVNCustomSearchBarIconAlignCenter;

            [_searchBar setPlaceholder:K_CC_LOCAL_STR(@"home.search")];// 搜索框的占位符

            _searchBar.placeholderColor = K_CC_COLOR_STRING(@"#999999");

            _searchBar.delegate = self; //设置代理

            _searchBar.isHiddenCancelButton = YES;

            _searchBar.tintColor = K_CC_COLOR_STRING(@"#999999");

            [_searchBar sizeToFit];

            

        }

        return _searchBar;

      第二步:添加到相应的view上

      

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_CC_SCREEN_WIDTH, 44)];

        headerView.backgroundColor = [UIColor clearColor];

        [headerView addSubview:self.searchBar];

        self.distributeTable.tableHeaderView = headerView;

      第三步:添加相关的搜索代理逻辑处理

      

    #pragma mark - EVNCustomSearchBarDelegate -

    - (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText {

        if (searchText.length > 0) {

            // 根据模型属性搜索结果

            self.searchList=[NSMutableArray arrayWithArray:[[NDSearchTool tool] searchWithFieldArray:@[@"username"] inputString:searchText inArray:self.dataAddressList]];

            self.isSearch = YES;

            

        }else{

            self.isSearch = NO;

            self.currentSeasSearchItem=-1;

            //清空搜索内容Id

            [self.searchList removeAllObjects];

        }

        [self.distributeTable reloadData];

    }

    - (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)

        {

            NSArray *tempArray;

            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.%@ contains[c] %@", fieldString, inputString];

            tempArray = [array filteredArrayUsingPredicate:scopePredicate];

            

            for (NSObject *object in tempArray)

            {

                if (![backArray containsObject:object])

                {

                    [backArray nd_addObj:object];

                }

            }

        }

        

        return backArray;

    }

      完成这些就可以实现动态的模糊搜索,大功告成。

  • 相关阅读:
    2018.08.02
    加油。
    2018.07.08
    2018.06.22
    LeetCode #169. Majority Element 数组 摩尔投票法
    LeetCode #119 Pascal's Triangle II 数组 滚动数组
    LeetCode #845 Longest Mountain in Array 数组 线性DP
    LeetCode #41 First Missing Positive 数组
    LeetCode #384 Shuffle an Array 数组 洗牌算法
    LeetCode #189 Rotate Array 数组 双变量 双指针
  • 原文地址:https://www.cnblogs.com/bigant9527/p/14685872.html
Copyright © 2020-2023  润新知