uisearchbar点击时下边有阴影原来是苹果自带的控件UISearchDisplayController
2013-06-18 10:45
471 查看
转载自:http://www.devdiv.com/uisearchdisplaycontroller_-blog-243602-50843.html
先把我已经实现的代码岾出来吧,不保证完全准确,但确实实现了:
注:头文件中要加上两个委托<UISearchBarDelegate,UISearchDisplayDelegate>
-(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(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*subviewindisplayCon.searchBar.subviews)
{
if([subviewisKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subviewremoveFromSuperview];
break;
}
}
return[displayCon.searchBarautorelease];
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchString:(NSString*)searchString{
//NSString*scope=[[self.searchDisplayController.searchBarscopeButtonTitles]
//objectAtIndex:[self.searchDisplayController.searchBarselectedScopeButtonIndex]];
//[selffilterContentForSearchBarText:searchStringscope:scope];
returnYES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchScope:(NSInteger)searchOption{
//NSString*scope=[[self.searchDisplayController.searchBarscopeButtonTitles]objectAtIndex:searchOption];
//[selffilterContentForSearchBarText:self.searchDisplayController.searchBar.textscope:scope];
returnYES;
}
其实这个应该是官方就有,这里只是记录给自个看看
其实应该比较好理解,UISearchDisplayController里面自带的tableView是继承我们本来创建的tableView,只是对数据进行更换,就能过显示出来了。
通过这样判断
加入回调
先把我已经实现的代码岾出来吧,不保证完全准确,但确实实现了:
注:头文件中要加上两个委托<UISearchBarDelegate,UISearchDisplayDelegate>
-(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(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*subviewindisplayCon.searchBar.subviews)
{
if([subviewisKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subviewremoveFromSuperview];
break;
}
}
return[displayCon.searchBarautorelease];
}
- (CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section |
2 | { |
3 | return tableView==self.searchDisplayController.searchResultsTableView?0:22; |
4 | } |
//NSString*scope=[[self.searchDisplayController.searchBarscopeButtonTitles]
//objectAtIndex:[self.searchDisplayController.searchBarselectedScopeButtonIndex]];
//[selffilterContentForSearchBarText:searchStringscope:scope];
returnYES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchScope:(NSInteger)searchOption{
//NSString*scope=[[self.searchDisplayController.searchBarscopeButtonTitles]objectAtIndex:searchOption];
//[selffilterContentForSearchBarText:self.searchDisplayController.searchBar.textscope:scope];
returnYES;
}
其实这个应该是官方就有,这里只是记录给自个看看
其实应该比较好理解,UISearchDisplayController里面自带的tableView是继承我们本来创建的tableView,只是对数据进行更换,就能过显示出来了。
通过这样判断
1 | - (CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section |
2 | { |
3 | return tableView==self.searchDisplayController.searchResultsTableView?0:22; |
4 | } |
1 | UISearchDisplayDelegate |
[代码]c#/cpp/oc代码:
1 | UISearchDisplayController *m_searchDisplayController; |
2 |
3 | m_searchDisplayController=[[UISearchDisplayController //传入创建的tableVIew |
4 | m_searchDisplayController. delegate = self; |
5 | m_searchDisplayController.searchResultsDataSource |
6 | m_searchDisplayController.searchResultsDelegate |
[代码]c#/cpp/oc代码:
01 | #pragma |
02 | #pragma |
03 |
04 | - ( void )filteredListContentForSearchText:(NSString*)searchText scope:(NSString*)scope |
05 | { |
06 | if (nil==m_filteredListContent) |
07 | m_filteredListContent=[NSMutableArray new ]; |
08 | [m_filteredListContent removeAllObjects]; |
09 |
10 | for (NSString* in groups) |
11 | { |
12 | NSArray |
13 | for (NSMutableDictionary in contactSection) |
14 | { |
15 | if ([[[eObj objectForKey: @"name" ] |
16 | { |
17 | [m_filteredListContent addObject:eObj]; |
18 | } |
19 | } |
20 | } |
21 | } |
22 |
23 | #pragma |
24 | #pragma |
25 |
26 | - ( void )searchDisplayControllerWillBeginSearch:(UISearchDisplayController |
27 |
28 | UISearchBar |
29 |
30 | [searchBar |
31 |
32 | for (UIView in searchBar.subviews){ |
33 |
34 | if ([subView isKindOfClass:UIButton. class ]){ |
35 |
36 | [(UIButton*)subView setTitle: @"取消11111" forState:UIControlStateNormal]; |
37 |
38 | } |
39 |
40 | } |
41 |
42 | } |
43 |
44 | //- (BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchString:(NSString*)searchString |
45 | // |
46 | //{ |
47 | // |
48 | //// |
49 | //// |
50 | //// |
51 | // |
52 | // |
53 | // |
54 | // |
55 | // |
56 | // |
57 | // |
58 | // |
59 | // |
60 | // |
61 | // |
62 | // |
63 | // } |
64 | // |
65 | // } |
66 | // |
67 | //} |
68 |
69 | - (BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchString:(NSString*)searchString |
70 | { |
71 | [self |
72 |
73 | return YES; |
74 | } |
75 |
76 | - (BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchScope:(NSInteger)searchOption |
77 | { |
78 | [self |
79 |
80 | return YES; |
81 | } |
相关文章推荐
- 【IOS 开发学习总结-OC-53】★★ios开发UI 控件——UISearchBar与UISearchDisplayController
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和UISearchDisplayController
- UISearchBar和 UISearchDisplayController的使用
- iOS - 搜索框UISearchController的使用(iOS8.0之后替代UISearchBar + UISearchDisplayController的组合)
- UISearchBar和UISearchDisplayController
- UISearchController VS UISearchBar and UISearchDisplayController
- UISearchBar和UISearchDisplayController
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- IOS--常用控件--UISearchBar和UISearchDisplayController
- UISearchDisplayController UISearchBar
- UISearchBar 和 UISearchDisplayController 没有在UITableView 的headerView 导致searchResultsTableView的frame出问题
- 关于UISearchDisplayController和 uisearchBar使用时遇到的问题
- iOS UISearchBar和UISearchDisplayController
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和UISearchDisplayController
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar和UISearchDisplayController