您的位置:首页 > 产品设计 > UI/UE

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];
}

-
(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section
2
{
3
return
tableView==self.searchDisplayController.searchResultsTableView?0:22;
4
}
-(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,只是对数据进行更换,就能过显示出来了。

通过这样判断

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
alloc]initWithSearchBar:m_ctable.m_searchbar.m_searchbarcontentsController:self];
//传入创建的tableVIew
4
m_searchDisplayController.
delegate
=
self;
5
m_searchDisplayController.searchResultsDataSource
=self;
6
m_searchDisplayController.searchResultsDelegate
=self;

[代码]c#/cpp/oc代码:

01
#pragma
mark-
02
#pragma
markContentFiltering
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*
str
in
groups)
11
{
12
NSArray
*contactSection=[contactTitlesobjectForKey:str];
13
for
(NSMutableDictionary
*eObj
in
contactSection)
14
{
15
if
([[[eObj
objectForKey:
@"name"
]
uppercaseString]rangeOfString:[searchTextuppercaseString]].length>0)
16
{
17
[m_filteredListContent
addObject:eObj];
18
}
19
}
20
}
21
}
22
23
#pragma
mark-
24
#pragma
markUISearchDisplayControllerDelegateMethods
25
26
-
(
void
)searchDisplayControllerWillBeginSearch:(UISearchDisplayController
*)controller{
27
28
UISearchBar
*searchBar=self.searchDisplayController.searchBar;
29
30
[searchBar
setShowsCancelButton:YESanimated:YES];
31
32
for
(UIView
*subView
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
////
[selffilterContentForSearchText:searchString];
49
////
50
////
if([filteredListPinYincount]==0){
51
//
52
//
UITableView*tableView1=self.searchDisplayController.searchResultsTableView;
53
//
54
//
for(UIView*subviewintableView1.subviews){
55
//
56
//
if([subviewclass]==[UILabelclass]){
57
//
58
//
UILabel*lbl=(UILabel*)subview;//svchangedtosubview.
59
//
60
//
lbl.text=@"没有结果";
61
//
returnYES;
62
//
63
//
}
64
//
65
//
}
66
//
returnYES;
67
//}
68
69
-
(BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchString:(NSString*)searchString
70
{
71
[self
filteredListContentForSearchText:searchStringscope:[[self.searchDisplayController.searchBarscopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBarselectedScopeButtonIndex]]];
72
73
return
YES;
74
}
75
76
-
(BOOL)searchDisplayController:(UISearchDisplayController*)controllershouldReloadTableForSearchScope:(NSInteger)searchOption
77
{
78
[self
filteredListContentForSearchText:[self.searchDisplayController.searchBartext]scope:[[self.searchDisplayController.searchBarscopeButtonTitles]objectAtIndex:searchOption]];
79
80
return
YES;
81
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: