搜索栏+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 = [ super init ]) {
       
_parentController = controller;
    }
   
return self ;
}
.
 
声明collectionView
 
- ( void )prepareView {
   
self . frame = CGRectMake ( 0 , 0 , SCREEN_WIDTH , SCREEN_HEIGHT );
   
self . backgroundColor = NF_Color_C20 ;
   
_page = 1 ;
   
_gameRoomDataArray = [ NSMutableArray array ];
   
_sessionArray = [ NSMutableArray array ];
   
UICollectionViewFlowLayout *layout = [[ UICollectionViewFlowLayout alloc ] init ];
   
    layout.
scrollDirection = UICollectionViewScrollDirectionVertical ;
    layout.
sectionInset = UIEdgeInsetsMake ( 10 , 9 , 15 , 9 );
    layout.
minimumInteritemSpacing = 9 ;
    layout.
minimumLineSpacing = 9 ;
   
   
   
self . gameRoomCollectionView = [[ UICollectionView alloc ] 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 . gameRoomCollectionView registerClass :[ IDSGameRoomHomePageChangeCell class ] forCellWithReuseIdentifier : @"THEcellid" ];
   
self . gameRoomCollectionView . showsVerticalScrollIndicator = NO ;
   
self . gameRoomCollectionView . showsHorizontalScrollIndicator = NO ;
   
   
MJRefreshFooter *footer = [ IDSRefresh footerWithRefreshingTarget : self refreshingAction : @selector (footerVoid)];
   
_gameRoomCollectionView . mj_footer = footer;
   
self . gameRoomCollectionView . mj_footer . hidden = YES ;
   
    [
self addSubview : _gameRoomCollectionView ];
    [
self addSearchBar ];
    [
_parentController . view addSubview : self ];

}
.
 
搜索栏初始化方法:
 
-( void )addSearchBar{
   
if (! self . searchBar ) {
   
       
self . searchBarBoundsY = 20 ;
       
self . searchBar = [[ UISearchBar alloc ] initWithFrame : CGRectMake ( 0 , self . searchBarBoundsY , [ UIScreen mainScreen ]. 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 . searchBar setAutocorrectionType : UITextAutocorrectionTypeNo ];
        [
self . searchBar setAutocapitalizationType : UITextAutocapitalizationTypeNone ];
        [[
UITextField appearanceWhenContainedIn :[ UISearchBar class ], nil ] setTextColor :[ UIColor blackColor ]];
       
    }
   
   
if (![ self . searchBar isDescendantOfView : self ]) {
        [
self addSubview : self . searchBar ];
    }
   
    [
self . searchBar setShowsCancelButton : YES animated : NO ];
    [
self . searchBar becomeFirstResponder ];
   
if (![ _queryNoticeTimer isValid ]) {
        [
self startQueryTimer ];
    }
}
 
.
隐藏键盘增加取消键方法:
 
- ( void )hiddenKeyBoard
{
    [
self . searchBar setShowsCancelButton : YES animated : NO ];
    [
self . searchBar resignFirstResponder ]; //searchBar 失去焦点
   
UIButton *cancelBtn = [ self . searchBar valueForKey : @"cancelButton" ]; // 首先取出 cancelBtn
    cancelBtn.
enabled = YES ; // enabled 设置为 yes
}
.
 
滑动时,隐藏键盘:
 
- ( void )scrollViewDidScroll:( UIScrollView *)scrollView
{
    [
self hiddenKeyBoard ];
}
.
 
移除View 方法:
 
- ( void )removeAllSubviews
{
   
   
for ( UIView *oneView in self . subviews ) {
        [oneView
removeFromSuperview ];
    }
}

- (
void )removeView
{
   
    [
self removeAllSubviews ];
    [
self removeFromSuperview ];
}
.
 
搜索代理:
 
- ( void )searchBar:( UISearchBar *)searchBar textDidChange:( NSString *)searchText{
    [
self stopQueryTimer ];
   
   
if (searchText. length > 0 ) {
       
       
self . searchBarActive = YES ;
       
_searchWord = searchText;
        [
self startQueryTimer ];
        [
self . gameRoomCollectionView reloadData ];
    }
else {
       
self . searchBarActive = NO ;
        [
self removeEmptyView ];
        [
self . gameRoomDataArray removeAllObjects ];
        [
self . gameRoomCollectionView reloadData ];
    }
}

- (
void )searchBarCancelButtonClicked:( UISearchBar *)searchBar{
    [
self cancelSearching ];
}
- (
void )searchBarSearchButtonClicked:( UISearchBar *)searchBar{
    [
self hiddenKeyBoard ];
   
}
- (
void )searchBarTextDidBeginEditing:( UISearchBar *)searchBar{

    [
self . searchBar setShowsCancelButton : YES animated : YES ];
}
- (
void )searchBarTextDidEndEditing:( UISearchBar *)searchBar{
   
    [
self hiddenKeyBoard ];
    [
self dataRequest ];
}
-(
void )cancelSearching{

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

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

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

 

转载于:https://www.cnblogs.com/firstrate/p/7222592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值