UISearchBar實(shí)現(xiàn)本頁面搜索(跳轉(zhuǎn)功能)

UISearchBar實(shí)現(xiàn)本頁面搜索跳轉(zhuǎn)功能,這個(gè)效果是iOS8.0之前的主流的效果

一、先看個(gè)效果

2016-07-12_14-37-01.gif

二、思路分析和代碼實(shí)現(xiàn)

這個(gè)效果的實(shí)現(xiàn),我覺的主要有二個(gè)難點(diǎn)(1.從大量的數(shù)據(jù)源中匹配到符合要求的;2.源頁面和搜索到的頁面的點(diǎn)擊事件)。

2-1.解決第一個(gè)問題(從大量的數(shù)據(jù)源中匹配到符合要求)
注意了,開始上代碼了
 #pragma mark UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSString* str = searchText;
    //這一句代碼的妙處在于:可以用self.string的length來進(jìn)行判斷是原數(shù)據(jù)還是搜索的數(shù)據(jù)
    self.string = str;
    NSLog(@"%@",str);
    NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@ ",str];
//清空搜索數(shù)組
    [self.resultArray removeAllObjects];
    self.resultArray =  [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:namePredicate]];
    [self.collectionView reloadData];

}

2-2.源頁面和搜索到的頁面的點(diǎn)擊事件

需要注意的是:DataSource和點(diǎn)擊事件的delegate二個(gè)部分都可以通過self.string來進(jìn)行判斷。

#pragma mark--UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.string.length == 0) {
        return _dataArray.count;
    }else{
        return self.resultArray.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

InvestmentsViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    if (self.string.length == 0) {
        cell.lable.text = _dataArray[indexPath.item];
        NSLog(@"%@",cell);
    }else{
        cell.lable.text =     self.resultArray[indexPath.item];
        NSLog(@"%@",cell);
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld %ld",indexPath.section,indexPath.item);
    testController *test = [[testController alloc] init];
    if (self.string.length == 0) {
        test.str = _dataArray[indexPath.item];
    }else{
        test.str = self.resultArray[indexPath.item];
    }

    [self.navigationController pushViewController:test animated:YES];
}

三.結(jié)束

以上的效果的實(shí)現(xiàn),代碼已經(jīng)實(shí)現(xiàn)。之前本人用UISearchController實(shí)現(xiàn)了另外一種的本頁面的實(shí)現(xiàn)的效果,參考那篇文章看,歡迎各位同仁批評指正,共同進(jìn)步。有什么問題可以通過QQ聯(lián)系1312940166。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,380評論 4 61
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 機(jī)器學(xué)習(xí) 監(jiān)督學(xué)習(xí)回歸分析分類問題 非監(jiān)督學(xué)習(xí)分組問題降低維度 一元線性回歸 多項(xiàng)式回歸 因?yàn)閄沒有排序,畫出來的...
    雷小廝閱讀 3,007評論 2 0
  • 杏花微雨,猶賞西風(fēng)凋碧樹。紅斷香消,片片和酒數(shù)。佇窗落髻,年年春辭舊。念誰去?歲歲依寒,日日空悠度。
    廣陌閱讀 353評論 7 5

友情鏈接更多精彩內(nèi)容