zoukankan      html  css  js  c++  java
  • 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;

    }

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

  • 相关阅读:
    【Matlab】去除图片周围空白区域(plot subplot)
    使用nbdev进行jupyter项目的开发
    如何绘制符合打印标准的图形
    如何使用Python完成视频的快速剪辑
    如何查看和修改论文图片的打印尺寸
    使用TMUX替代screen工具
    Emacs设置包管理器以及镜像
    Emacs的配置文件
    Emacs Windows的设置
    数据科学新的工具Julia
  • 原文地址:https://www.cnblogs.com/bigant9527/p/14685872.html
Copyright © 2011-2022 走看看