效果图如下:

 
1.h文件声明方法:
 
@interface IDSGameRoomSearchPage : UIView

@property (nonatomic,weak) BaseViewController *parentController;

- (instancetype)initWithParentControler:(BaseViewController *)controller;

- (void)prepareView;

@end

 
.
初始化方法
- (instancetype)initWithParentControler:(BaseViewController *)controller
{
    if (self = [superinit]) {
        _parentController = controller;
    }
    returnself;
}
.
 
声明collectionView
 
- (void)prepareView {
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backgroundColor = NF_Color_C20;
    _page = 1;
    _gameRoomDataArray = [NSMutableArrayarray];
    _sessionArray = [NSMutableArrayarray];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];
   
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(10, 9, 15, 9);
    layout.minimumInteritemSpacing = 9;
    layout.minimumLineSpacing = 9;
   
   
    self.gameRoomCollectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(0, 44+20, SCREEN_WIDTH, SCREEN_HEIGHT-44-20) collectionViewLayout:layout];
    self.gameRoomCollectionView.delegate = self;
    self.gameRoomCollectionView.dataSource = self;
    self.gameRoomCollectionView.backgroundColor = NF_Color_C16;
    [self.gameRoomCollectionViewregisterClass:[IDSGameRoomHomePageChangeCellclass] forCellWithReuseIdentifier:@"THEcellid"];
    self.gameRoomCollectionView.showsVerticalScrollIndicator = NO;
    self.gameRoomCollectionView.showsHorizontalScrollIndicator = NO;
   
    MJRefreshFooter *footer = [IDSRefreshfooterWithRefreshingTarget:selfrefreshingAction:@selector(footerVoid)];
    _gameRoomCollectionView.mj_footer = footer;
    self.gameRoomCollectionView.mj_footer.hidden = YES;
   
    [selfaddSubview:_gameRoomCollectionView];
    [selfaddSearchBar];
    [_parentController.viewaddSubview:self];

}

.
 
搜索栏初始化方法:
 
-(void)addSearchBar{
    if (!self.searchBar) {
   
        self.searchBarBoundsY = 20;
        self.searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,self.searchBarBoundsY, [UIScreenmainScreen].bounds.size.width, 44)];
        self.searchBar.searchBarStyle      = UISearchBarStyleMinimal;
        self.searchBar.tintColor            = NF_Color_C27;
        self.searchBar.barTintColor        = NF_Color_C1;
        self.searchBar.delegate            = self;
        self.searchBar.placeholder          = @"搜索房号/房间名";
        [self.searchBarsetAutocorrectionType:UITextAutocorrectionTypeNo];
        [self.searchBarsetAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [[UITextFieldappearanceWhenContainedIn:[UISearchBarclass], nil] setTextColor:[UIColorblackColor]];
       
    }
   
    if (![self.searchBarisDescendantOfView:self]) {
        [selfaddSubview:self.searchBar];
    }
   
    [self.searchBarsetShowsCancelButton:YESanimated:NO];
    [self.searchBarbecomeFirstResponder];
    if (![_queryNoticeTimerisValid]) {
        [selfstartQueryTimer];
    }
}
 
.
隐藏键盘增加取消键方法:
 
- (void)hiddenKeyBoard
{
    [self.searchBarsetShowsCancelButton:YESanimated:NO];
    [self.searchBarresignFirstResponder]; //searchBar失去焦点
    UIButton *cancelBtn = [self.searchBarvalueForKey:@"cancelButton"]; //首先取出cancelBtn
    cancelBtn.enabled = YES; //把enabled设置为yes
}
.
 
滑动时,隐藏键盘:
 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [selfhiddenKeyBoard];
}
.
 
移除View 方法:
 
- (void)removeAllSubviews
{
   
    for (UIView *oneView inself.subviews) {
        [oneView removeFromSuperview];
    }
}

- (void)removeView
{
   
    [selfremoveAllSubviews];
    [selfremoveFromSuperview];
}

.
 
搜索代理:
 
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [selfstopQueryTimer];
   
    if (searchText.length>0) {
       
        self.searchBarActive = YES;
        _searchWord = searchText;
        [selfstartQueryTimer];
        [self.gameRoomCollectionViewreloadData];
    }else{
        self.searchBarActive = NO;
        [selfremoveEmptyView];
        [self.gameRoomDataArrayremoveAllObjects];
        [self.gameRoomCollectionViewreloadData];
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [selfcancelSearching];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [selfhiddenKeyBoard];
   
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

    [self.searchBarsetShowsCancelButton:YESanimated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
   
    [selfhiddenKeyBoard];
    [selfdataRequest];
}
-(void)cancelSearching{

    [selfremoveView];
}

.
 
CollectionView 代理就省略啦
 
需要的代理:
<UICollectionViewDelegate,UICollectionViewDataSource,UISearchBarDelegate>
 
释放内存:
 
- (void)dealloc
{
    IDSLOG(@"IDSGameRoomSearchPage Dealloc.");
    [selfreleaseSelf];
}

- (void)releaseSelf
{
    self.gameRoomCollectionView.delegate = nil;
    self.gameRoomCollectionView.dataSource = nil;
    _gameRoomCollectionView = nil;
    self.searchBar = nil;
    [selfstopQueryTimer];
}

.
 
增加计时器方法:
 
Ps : 加入定时器是由于搜索会即时搜索,为了防止实时调用请求,给用户0.5s 时间不操作再请求网络,以防止用户输入 abc ,请求了3次网络,依次是搜索 a , ab, abc ,给用户0.5秒延时输入单词。
 
staticCGFloat sIntervalTime = 0.5f; //定时刷新时间间隔
- (void)startQueryTimer
{
    [selfstopQueryTimer];
   
    if (nil == _queryNoticeTimer) {
        _queryNoticeTimer = [NSTimerscheduledTimerWithTimeInterval:sIntervalTime
                                                             target:self
                                                           selector:@selector(dataRequest)
                                                           userInfo:nilrepeats:NO];
    }
}

- (void)stopQueryTimer
{
    if (self.queryNoticeTimer) {
       
        [self.queryNoticeTimerinvalidate];
        _queryNoticeTimer = nil;
    }
}

 
关于搜索文字,关键字变成红色是引用富文本方式实现,方法如下:
 
_searchWord 为搜索关键字,要传全局变量进入方法当中
 
 
- (void)setLineSpacing:(CGFloat)spacing label:(UILabel *)label
{
    if (!label.text.length) {
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:label.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc] init];
    [paragraphStyle setLineSpacing:spacing];
    [attributedString addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle range:NSMakeRange(0, [label.textlength])];
    if (_searchWord) {
        NSRange redRange = [[ [attributedString string] lowercaseString] rangeOfString:[_searchWordlowercaseString]];
        if (redRange.length <= [label.textlength]) {
            [attributedString setAttributes:@{NSForegroundColorAttributeName:NF_Color_C19,NSFontAttributeName:[UIFontsystemFontOfSize:Near_Final_Font_T9]}range:redRange];
        }
    }
   
   
    [label setAttributedText:attributedString];
    label.lineBreakMode = NSLineBreakByCharWrapping;
    [label sizeToFit];
}
 
明天说一下 collectionView cell 的实现方式
 
 

 

搜索栏+collectionView实现的更多相关文章

  1. 用collectionview实现瀑布流-转(后面附demo,供参考)

    算法总体思路 先说一下总体上的思路.既然图片的大小.位置各不一样,我们很自然地会想到需要算出每个item的frame,然后把这些frame赋值给当前item的UICollectionViewLayou ...

  2. CollectionView水平和竖直瀑布流的实现

    最近在项目中需要实现一个水平的瀑布流(即每个Cell的高度是固定的,但是长度是不固定的),因为需要重写系统 UICollectionViewLayout中的一些方法通过计算去实现手动布局,所以本着代码 ...

  3. tableViewCell嵌套collectionView,动态高度

    方法有很多,有通过内容高度,经过代理回调,刷新的,甚至还有计算cell个数,然后根据cell大小计算的,这里推荐iOS 8新特性,通过AutoLayout,利用内容将cell撑起来; 关键代码: vi ...

  4. iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调

    UICollectionView的布局是可以自己定义的,在这篇博客中先在上篇博客的基础上进行扩充,我们先使用UICollectionViewFlowLayout,然后好好的介绍一下UICollecti ...

  5. WPF CollectionViewSource CollectionView

    CollectionView 通俗讲就是可以对你绑定的集合可以进行 分组,排序 等功能 CollectionViewSource  根据字面意思是xxx的数据源 详细的介绍还是看 http://www ...

  6. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  7. collectionview cell吸顶效果

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Hiragino Sans GB"; color: #cf8724 } ...

  8. collectionview使用

    创建UICollectionViewFlowLayout 对象来设置相关的布局,包括itemSize,headerReferenceSize,sectionInset.设置对应的布局大小,相关的和顶部 ...

  9. collectionView

    // /* UICollectionView 类是iOS6 新引进的API,用于展示集合视图, 布局更加灵活,可实现多列布局,用法类似于UITableView类. - 更新视图: [collectio ...

随机推荐

  1. mysql 8 远程连接注意事项

    1.首先如果你是在windows平台下连接linux下的mysql 那么请确保linux下的防火墙开放3306端口 vi /etc/sysconfig/iptables在配置文件里增加一行 -A RH ...

  2. 看看是不是你想要的:pycharm永久激活!!!

    pycharm是很强大的开发工具,但是每次注册着实让人头疼.网络上很多注册码.注册服务器等等.但都只是一年或者不能用:为次有如下解决方案.亲测有效!!! 如果想让pycharm永久被激活,比如截止日到 ...

  3. Method for training dynamic random access memory (DRAM) controller timing delays

    Timing delays in a double data rate (DDR) dynamic random access memory (DRAM) controller (114, 116) ...

  4. JavaScript严谨模式(Strict Mode)提升开发效率和质量

    http://flandycheng.blog.51cto.com/855176/982719 http://my.oschina.net/Jsiwa/blog/272761

  5. Eclipse中设置自定义文档签名

    今天第一次认真学习eclipse的使用,看到自定义文档签名,步骤如下: 1.点击window->preferences->java->Code Style->Code Temp ...

  6. phpStudy2018安装完成之后,Apache刚启动就关闭(PHPStudy 从别的电脑迁移过来)

    原文:phpStudy2018安装完成之后,Apache刚启动就关闭 版权声明:本文为博主原创文章,未经博主允许不得转载.用于学习总结等. https://blog.csdn.net/pjz16102 ...

  7. Matlab Tricks(二十八)—— 笛卡尔积的实现

    笛卡尔积在数学上是一种二元关系,笛卡尔积作用的双方是两个集合,作用的结果是一个新的集合. A×B={(a,b)|a∈Aandb∈B} 现有两向量: >> p = [1, 5, 10]; & ...

  8. ubuntu设置root密码及 Xftp连接linux(ubuntu)时提示ssh服务器拒绝了密码,请再试一次

    原文:ubuntu设置root密码及 Xftp连接linux(ubuntu)时提示ssh服务器拒绝了密码,请再试一次 alt+f2,在弹出的运行窗口中输入:gnome-terminal sudo pa ...

  9. BZOJ 1483 HNOI2009 梦幻布丁 名单+启示录式的合并

    标题效果:特定n布丁.每个人都有一个颜色布丁,所有的布丁反复有一定的颜色变化的颜色,颜色反复询问的段数 数据范围:n<=10W 色彩数<=100W 启发式合并名单0.0 从来不写清楚 实际 ...

  10. Spring boot将配置属性注入到bean 专题

    https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...