UISearchView不显示search值

在我的TableView扩展点击sections.now我使用Uisearchbarsearch表中的部分…它给了我的UIsearchbar但search不能采取…我认为问题是在numberOfRowsInSection .please检查我得到错误的地方..为什么search栏不工作

输入一天

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.mySections count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger rows = 0; if ([self tableView:tableView canCollapseSection:section] || (tableView == self.searchDisplayController.searchResultsTableView) ) { if ([expandedSections containsIndex:section] ) { NSString *key = [self.mySections objectAtIndex:section]; NSArray *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0]; return [dataInSection count]; } return 1; } else{ rows = [self.searchResults count]; return rows; } return 1; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section { NSString *key = [self.mySections objectAtIndex:section]; return [NSString stringWithFormat:@"%@", key]; } - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate]; } -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { UISearchBar * searchBar = [controller searchBar]; [self filterContentForSearchText:searchString scope:[[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]]]; return YES; } -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { UISearchBar * searchBar = [controller searchBar]; [self filterContentForSearchText:[searchBar text] scope:[[searchBar scopeButtonTitles] objectAtIndex:searchOption]]; return YES; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ; } // Configure the cell... if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) { cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; }else { NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSString *key = [self.mySections objectAtIndex:section]; NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0]; NSArray *array=dataForSection.allKeys; cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row]; cell.detailTextLabel.text=[dataForSection valueForKey:[array objectAtIndex:indexPath.row]]; } return cell; } 

search之后,您不会重新载入您的表格

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; self.mySections = [self.mySections filteredArrayUsingPredicate:resultPredicate]; [myTableView reloadData]; } 

编辑

你没有设置详细的文字标签

 if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) { cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row]; cell.detailTextLabel.text=[dataForSection valueForKey:[self.searchResults objectAtIndex:indexPath.row]]; } 

并search后,你现在正在获得标题行,因为在你的代码

  rows = [self.searchResults count]; return rows; 

它总是返回零值。 所以只是做它return 1;

并按照您的要求做其他事情,

我会build议你不要使用不同的代码,以便在表search之前和search后。像if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])

只是使用相同的代码都..只做数组和字典更改..

原来

 tableAry = globalAry; 

然后search

 tableAry = searchedAry;