標籤: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