Iphone 如何在没有界面生成器的UIViewController中实现UISearchDisplayController
我有一个Iphone 如何在没有界面生成器的UIViewController中实现UISearchDisplayController,iphone,cocoa-touch,uiviewcontroller,uisearchdisplaycontroller,Iphone,Cocoa Touch,Uiviewcontroller,Uisearchdisplaycontroller,我有一个UIViewController,它有一个分组的UITableView属性。我在代码中实例化了UITableView,但没有使用IB。我想将UISearchDisplayController连接到它,但找不到任何可以实现这一点的示例 这就是我所拥有的。 //已经在头文件中实现了UISearchDisplayDelegate //SearchBar UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0
UIViewController
,它有一个分组的UITableView
属性。我在代码中实例化了UITableView
,但没有使用IB。我想将UISearchDisplayController
连接到它,但找不到任何可以实现这一点的示例
这就是我所拥有的。
//已经在头文件中实现了UISearchDisplayDelegate
//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;
UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController; //Error here ?? self.searchDisplayController is ReadOnly and can't assign
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController release];
[myDisplayController release];
这似乎不起作用,UIViewController
的searchDisplayController属性似乎是只读的,我无法将myDisplayController
挂到它上。我真的不确定这样做是否正确
我一直在谷歌搜索如何在UIViewController
中使用UISearchDisplayController
。我能找到的所有示例都是如何使用IB将其实现到UITableViewController
,这不是我想要使用它的方式
有人能解释一下我是如何工作的吗?这是我使用的代码。将其放入UIViewController的viewDidLoad中,该UIViewController实例化其自己的UITableView。我认为您正在寻找的部分是添加搜索栏作为表视图的标题视图
UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
searchBarPlaceHolder = @"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;
self.theTableView.tableHeaderView = theSearchBar;
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:theSearchBar
contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
请参阅苹果文档:
讨论:此属性反映了
在Interface Builder中设置的searchDisplayController插座。如果
如果以编程方式创建搜索显示控制器,则
属性由搜索显示控制器在
它已初始化
这是一个例子UISearchDisplayController@luyuan:我认为
searchController
是UISearchDisplayController
对象,@Rayfleck忘记谈论它了。您可以像UIViewController
的UISearchDisplayController
那样创建1个UISearchDisplayController
对象。它的工作原理与UIViewController
@NtVietHung相同谢谢你的回答:)什么是searchBarPlaceHolder?哦,我想它只是一个NSString。我不明白,“如果你以编程方式创建搜索显示控制器”,这意味着什么?
@property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController