這裏我的代碼鑑於或者Controller.h的UISearchBar消失時,第一個字母按領域的UISearchBar,iOS7
#import "AppDelegate.h"
@interface SearchRsultsFanSideViewController : UIViewController<UISearchBarDelegate,UISearchDisplayDelegate>
{
}
@property (strong, nonatomic) IBOutlet UISearchBar *searchData;
@property (strong, nonatomic) UISearchDisplayController *controller;
在viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
searchResults=[[NSArray alloc]init];
self.controller = [[UISearchDisplayController alloc]initWithSearchBar:searchData contentsController:self];
self.controller.searchResultsDataSource = self;
self.controller.searchResultsDelegate = self;
}
委託方法的UISearchBar
#pragma Mark - SearchBar Delegate
- (void)filterData
{
searchResults = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", searchData.text];
NSArray *arrayaaa=[finalArray copy];
NSLog(@"The result Datas==%@",arrayaaa);
searchResults = [[arrayaaa filteredArrayUsingPredicate:predicate] mutableCopy];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self.searchDisplayController setActive:YES];
[self filterData];
}
有什麼用篩選數據的>? –
什麼是self.searchDisplayController? – Tendulkar
我使用了故事板這是你用的 –