zoukankan      html  css  js  c++  java
  • 搜索栏+collectionView实现

    效果图如下:

    20F79F50 2E92 4B71 9101 12076B333DBB

     
    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 的实现方式
     
     
  • 相关阅读:
    软件产品案例分析 ——华为软件开发云
    软件工程实践2017第一次作业
    软件工程实践2017结对第二次作业
    SDN第一次作业
    路由器工作原理
    Spring框架之springweb web源码完全解析
    Spring框架之springweb http源码完全解析
    Spring框架之jms源码完全解析
    Spring框架之AOP源码完全解析
    比特币里的计算机知识
  • 原文地址:https://www.cnblogs.com/firstrate/p/7222592.html
Copyright © 2011-2022 走看看