转载自:http://www.devdiv.com/uisearchdisplaycontroller_-blog-243602-50843.html
先把我已经实现的代码岾出来吧,不保证完全准确,但确实实现了:
注:头文件中要加上两个委托 <UISearchBarDelegate,UISearchDisplayDelegate>
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UISearchBar *searchBar = [[UISearchBaralloc]initWithFrame:
CGRectMake(0,0,310,
40)];
searchBar.placeholder=@"输入关键词!";
searchBar.delegate =self;
searchBar.keyboardType =UIKeyboardTypeDefault;
searchBar.barStyle =UIBarStyleDefault;
searchBar.backgroundColor = [UIColorlightGrayColor];
UISearchDisplayController *displayCon = [[UISearchDisplayControlleralloc]initWithSearchBar:searchBarcontentsController:self];
displayCon.delegate =self;
displayCon.searchResultsDataSource =self;
displayCon.searchResultsDelegate =self;
for (UIView *subviewin displayCon.searchBar.subviews)
{
if ([subviewisKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
return [displayCon.searchBarautorelease];
}
-
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section |
3 |
return tableView==self.searchDisplayController.searchResultsTableView?0:22; |
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// NSString *scope = [[self.searchDisplayController.searchBar scopeButtonTitles]
// objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]];
// [self filterContentForSearchBarText:searchString scope:scope];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// NSString *scope = [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption];
// [self filterContentForSearchBarText:self.searchDisplayController.searchBar.text scope:scope];
return YES;
}
其实这个应该是官方就有,这里只是记录给自个看看
其实应该比较好理解,UISearchDisplayController里面自带的tableView是继承我们本来创建的tableView,只是对数据进行更换,就能过显示出来了。
通过这样判断
1 |
-
(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section |
3 |
return tableView==self.searchDisplayController.searchResultsTableView?0:22; |
加入回调
1 |
UISearchDisplayDelegate |
[代码]c#/cpp/oc代码:
1 |
UISearchDisplayController
*m_searchDisplayController; |
3 |
m_searchDisplayController=[[UISearchDisplayController
alloc] initWithSearchBar:m_ctable.m_searchbar.m_searchbar contentsController:self]; |
4 |
m_searchDisplayController. delegate =
self; |
5 |
m_searchDisplayController.searchResultsDataSource
= self; |
6 |
m_searchDisplayController.searchResultsDelegate
= self; |
[代码]c#/cpp/oc代码:
02 |
#pragma
mark Content Filtering |
04 |
-
( void )filteredListContentForSearchText:(NSString*)searchText
scope:(NSString*)scope |
06 |
if (nil==m_filteredListContent) |
07 |
m_filteredListContent=[NSMutableArray new ]; |
08 |
[m_filteredListContent
removeAllObjects]; |
10 |
for (NSString*
str in groups) |
12 |
NSArray
* contactSection = [contactTitles objectForKey:str]; |
13 |
for (NSMutableDictionary
*eObj in contactSection) |
15 |
if ([[[eObj
objectForKey: @"name" ]
uppercaseString] rangeOfString:[searchText uppercaseString]].length>0) |
17 |
[m_filteredListContent
addObject:eObj]; |
24 |
#pragma
mark UISearchDisplayController Delegate Methods |
26 |
-
( void )searchDisplayControllerWillBeginSearch:(UISearchDisplayController
*)controller { |
28 |
UISearchBar
*searchBar = self.searchDisplayController.searchBar; |
30 |
[searchBar
setShowsCancelButton:YES animated:YES]; |
32 |
for (UIView
*subView in searchBar.subviews){ |
34 |
if ([subView
isKindOfClass:UIButton. class ]){ |
36 |
[(UIButton*)subView
setTitle: @"取消11111" forState:UIControlStateNormal]; |
48 |
////
[self filterContentForSearchText:searchString]; |
50 |
////
if ([filteredListPinYin count] == 0) { |
69 |
-
(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString |
71 |
[self
filteredListContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; |
76 |
-
(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption |
78 |
[self
filteredListContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; |