我已經花了數小時閱讀幾十個不同的博客和其他人q在這裏關於這個,所以,但我陷入了僵局。UISearchDisplayController - 如何實現並使其工作
我的問題是你如何使這件事情的工作。我已經在IB和代碼中試過了。
當我在IB中嘗試它時,即使將它拖到表格視圖(UITableViewController的)上,它也不會顯示,並且它會「捕捉」到表頭所在的表格中。但搜索欄永遠不會顯示。 (這是一個子問題,任何人都知道爲什麼?)
所以我試着在代碼中設置。我得到它分配,分配委託和數據源,但我從來沒有得到那個表示「沒有結果」的表視圖。酒吧出現了,我可以輸入它,但它總是顯示一張空桌子,即使我知道應該有結果。我懷疑它沒有顯示正確的表格,因爲單元格顯示「沒有結果」文本。失敗的
- (void)viewDidLoad {
[super viewDidLoad];
names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil];
results = [[NSMutableArray alloc] init];
UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
sb.tintColor = [UIColor greenColor];
searchTable.tableHeaderView = sb;
UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
[self setSearchDisplayController:sdc];
[sdc setDelegate:self];
[sdc setSearchResultsDataSource:self];}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchTable) {
// normal table view population
return [names count];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
return [results count];
}
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
if (tableView == self.searchTable) {
// normal table view population
cell.textLabel.text = [names objectAtIndex:indexPath.row];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
}
cell.textLabel.textColor = [UIColor blueColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchTable) {
// normal table view population
}
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
[results removeAllObjects]; // First clear the filtered array.
/*
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
for (NSString *str in names)
{
NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])];
if (result == NSOrderedSame)
{
[results addObject:str];
}
}
}
Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :).
如果沒有看到一些代碼,很難知道發生了什麼問題。 – conmulligan 2011-04-21 22:51:50