2

iPhone SDK 3.0具有這個方便的新類「UISearchDisplayController」,它可以輕鬆創建搜索欄,處理所有用戶輸入並顯示搜索結果。如何使用iPhone SDK 3.0中的UISearchDisplayController實現始終位於頂部的搜索欄

我使用它爲我的新的應用程序,但有一兩件事我想改變:

作爲默認的搜索欄應該顯示搜索結果的UITableView的頂部放。因此,當向下滾動結果列表時,搜索欄會消失。

我想實現的是將搜索欄始終放在頂部,並且在滾動TableView時,搜索欄應該保持在原來的位置。 (原因是:在我的應用程序中有時候會有很多搜索結果,所以有時用戶不得不向下滾動一下,當他意識到他必須更改搜索字符串時,我不想強​​迫他向後滾動一路)

什麼我已經做是增加搜索欄到的UINavigationController的觀點這是我的表視圖的「父視圖」,而不是將它添加到直接表視圖:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate]; 

searchBar = [[UISearchBar alloc] init]; 
searchBar.delegate = self; 

[searchBar sizeToFit]; 
CGFloat searchBarHeight = [searchBar frame].size.height; 
CGRect mainViewBounds = delegate.navController.view.bounds; 
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds), 
          CGRectGetMinY(mainViewBounds) + 20, 
          CGRectGetWidth(mainViewBounds), 
          searchBarHeight)]; 
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

[delegate.navController.view addSubview: searchBar]; 

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self]; 
searchDisplayController.searchResultsDataSource = self; 
searchDisplayController.searchResultsDelegate = self; 

...但在這裏我的問題是,表視圖留在搜索欄後面,總是有搜索結果的第一個TableViewCell,它隱藏在我的搜索欄後面。

有沒有辦法調整我的UITableViewController的UITableView的永久性,以便它在搜索欄下正確啓動?還有另一個問題:每次我觸摸搜索欄,UISearchDisplayController自動隱藏導航欄並調整UITableView的大小(它擴展到頂部),所以我認爲在這一點上,UISearchDisplayController會有一個大問題我的自定義大小TableView ...

非常感謝任何幫助!

回答

2

我沒有解決我頭頂的問題 - 但是我會說您最擔心的問題是用戶只需輕點屏幕頂部,然後將桌面縮放至頂部(如果它錯了,則更改搜索字詞)。

+0

謝謝您的回答!在此期間,我意識到,當輸入搜索詞時,UISearchDisplayController會自動鎖定搜索欄的位置並調整結果表視圖的大小......這幾乎就是我想要實現的內容...... – xoconoxtle 2009-07-31 20:00:18

1

我不希望滾動頁面上的位的一般解決方案是讓UITableView 在另一個視圖內。這個外部視圖包含兩件事情 - 頂部是一個條形圖,下面是一個略小於整頁的表格。

0

@interface:

UISearchDisplayController <UISearchBarDelegate> *searchDisplayController; 

@implementation:

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self]; 

searchDisplayController.delegate = self; 
searchBar.delegate = searchDisplayController; 
searchDisplayController.searchResultsDataSource = self; 
searchDisplayController.searchResultsDelegate = self; 
0

將這個代碼在你TableViewController:

self.tableView.frame = CGRectMake(0,searchDisplayController?.searchBar.bounds.size.height,320,480); 
相關問題