我有一個UITableViewController,我想在頂部添加一個UISearchBarController,所以它使用不同的表視圖(而不是UITableViewController的表視圖)進行搜索。將UISearchBarController添加到UITableViewController沒有IB
如何通過代碼初始化此代碼而不需要IB?
@interface mySearchController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
@property (nonatomic, retain) UISearchDisplayController *aSearchBarController;
@property (nonatomic, retain) UISearchBar *aSearchBar;
@end
- (id)init {
if ((self = [super init])) {
UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)];
self.aSearchBar = tempSearchBar;
self.aSearchBar.delegate = self;
[self.aSearchBar sizeToFit];
self.tableView.tableHeaderView = self.aSearchBar;
[self.aSearchBar release];
UISearchDisplayController *tempSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:aSearchBar contentsController:self];
self.searchDisplayController = tempSearchDisplayController;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
}
return self;
}
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Custom initialization.
}
return self;
}
謝謝,我添加了一些代碼的問題。我做對了嗎? – Jon 2012-01-18 21:00:10
這一切看起來都是正確的,只不過我會在'-viewDidLoad'中執行設置以保證您添加的表視圖不爲零。另外,您不需要像'initWithSearchBar:contentController:'處理那樣分配'self.searchDisplayController'。傳入'contentController'的控制器會自動擁有它的'searchDisplayController'屬性集。 – 2012-01-18 21:27:40
我現在確定我的IB應該是什麼樣子,我決定在IB中完成。基本上,我有一個4行的tableview,4行中的每一行都起着搜索的過濾器的作用。點擊4行時有一個複選標記。然後當你搜索時,它會顯示結果。我得到了一切工作,除了搜索後更新tableview。你能告訴我,如果我走在正確的道路上嗎?我已經上傳了IB文件格式的圖片。 – Jon 2012-01-18 22:29:34