【问题标题】:UISearchView not displaying search valuesUISearchView 不显示搜索值
【发布时间】:2012-12-21 06:35:15
【问题描述】:

在我的TableView 中,单击部分展开。现在我正在使用Uisearchbar 搜索表中的部分...它给了我UIsearchbar 但无法进行搜索... 我认为问题出在numberOfRowsInSection。请检查我哪里出错了.. 为什么搜索栏不起作用

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

【问题讨论】:

    标签: iphone ios uitableview uisearchbar


    【解决方案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;
    

    【讨论】:

    • 表搜索,但它不会在部分中.. 我只想搜索部分...
    • self.searchResults = [self.mySections filteredArrayUsingPredicate:resultPredicate];
    • 是的,我已经这样做了...问题是当我搜索 saybutter oil1 from figure...it will give me that..but i won't give the expanded key values of that butter oil...i think cellforrowatindexpath 的某些部分时需要更改
    • 搜索结果 ("BUTTER OIL,ANHYDROUS", "BUTTER,WHIPPED,WITH SALT", "BUTTER,WITH SALT", "MILK,BUTTERMILK,DRIED", "SOUR DRSNG,NON-BUTTERFAT,培养,填充奶油型")
    • 但是人的问题是他们正在从 plist 中搜索部分所在的位置,并且 1)如果我搜索部分,它没有创建部分..2)需要更改 celfforrowatindexpath]
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 2017-03-31
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多