iOS.UIKit.14.UITableView -- UISearchBar

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

1、案例介紹:一個具備搜尋功能的表視圖,01,02,03

圖01圖02圖03

2、Main.storyboard,04

圖04

3、.h

#import <UIKit/UIKit.h>@interface CQ23ViewController : UITableViewController<UISearchBarDelegate, UISearchDisplayDelegate>// 搜尋欄@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;// 所有隊伍集合@property (nonatomic, strong) NSArray *listTeams;// 查詢後的隊伍集合@property (nonatomic, strong) NSMutableArray *listFilterTeams;// 按條件查詢,載入查詢出的內容,給listFilterTeams賦值- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;@end

4、.m

 

#import "CQ23ViewController.h"@interface CQ23ViewController ()@end@implementation CQ23ViewController- (void)viewDidLoad{    [super viewDidLoad];        //設定搜尋欄ScopeBar隱藏    [self.searchBar setShowsScopeBar:NO];    [self.searchBar sizeToFit];        NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"team"                                           ofType:@"plist"];    //擷取屬性列表檔案中的全部資料    self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];        //初次進入查詢所有資料    [self filterContentForSearchText:@"" scope:-1];}- (void)viewDidUnload{    [self setSearchBar:nil];    [super viewDidUnload];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}#pragma mark Content Filtering- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;{        if([searchText length]==0)    {        //查詢所有        self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];        return;    }        NSPredicate *scopePredicate;    NSArray *tempArray ;        switch (scope) {        case 0: //英文            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];            tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];                        break;        case 1:            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.image contains[c] %@",searchText];            tempArray =[self.listTeams filteredArrayUsingPredicate:scopePredicate];            self.listFilterTeams = [NSMutableArray arrayWithArray:tempArray];                        break;        default:            //查詢所有            self.listFilterTeams = [NSMutableArray arrayWithArray:self.listTeams];            break;    }}#pragma mark --UITableViewDataSource 協議方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.listFilterTeams count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"CellIdentifier";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];    }            NSUInteger row = [indexPath row];    NSDictionary *rowDict = [self.listFilterTeams objectAtIndex:row];    cell.textLabel.text =  [rowDict objectForKey:@"name"];    cell.detailTextLabel.text = [rowDict objectForKey:@"image"];        NSString *imagePath = [rowDict objectForKey:@"image"];    imagePath = [imagePath stringByAppendingString:@".png"];    cell.imageView.image = [UIImage imageNamed:imagePath];        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        return cell;}#pragma mark --UISearchBarDelegate 協議方法- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{    // 當點擊取消按鈕,查詢所有    [self filterContentForSearchText:@"" scope:-1];}#pragma mark - UISearchDisplayController Delegate Methods- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{    // 當常值內容發生改變時候,向表視圖資料來源發出重新載入訊息    [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex];    //YES情況下表視圖可以重新載入    return YES;}- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{    // 當Scope Bar選擇發生變化時候,向表視圖資料來源發出重新載入訊息    [self filterContentForSearchText:self.searchBar.text scope:searchOption];    // YES情況下表視圖可以重新載入    return YES;}@end

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.