UISearchView不显示搜索值

时间:2012-12-21 06:35:15

标签: iphone ios uitableview uisearchbar

在我的TableView扩展点击部分。现在我使用Uisearchbar搜索表格中的部分...它给了我UIsearchbar但搜索不能被带走...... 我认为问题在numberOfRowsInSection。请检查我出错的地方.. 为什么搜索栏不起作用

enter day

- (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;
}

1 个答案:

答案 0 :(得分:1)

搜索

后,您没有重新加载表格
- (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]];
}  

在搜索之后,你现在正在获取标题行,因为在你的代码中

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

它总是返回零值。所以就这样做return 1;

并根据您的要求做其他事情,

我建议你不要在表搜索之前和搜索之后使用不同的不同代码。就像if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])

只需对两者使用相同的代码,并仅在数组和字典中进行更改。

最初

tableAry = globalAry;

搜索后

tableAry = searchedAry;