0

我的搜索栏中有两个范围:城市和教堂。每次我执行搜索时,两个范围都会显示空白单元格,而它们确实有搜索结果。如何显示它们对应的单元格名称?谢谢!

我认为问题出在我的 cellForRowAtIndexPath

if (tableView == self.searchDisplayController.searchResultsTableView) {
    if ([searchBar.scopeButtonTitles isEqual: @"City"]) {
        cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Places"];
    }

    else if ([searchBar.scopeButtonTitles isEqual: @"Church"]) {
        cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Church"];
    }
}

else {
    ...
}

[cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
return cell;

这是我的搜索:

- (void)filterListForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    [filteredResult removeAllObjects];

    if ([scope isEqualToString: @"City"]) {
        for (NSDictionary *dictionarySearch in allRegion) {
            for (NSDictionary *citySearch in [dictionarySearch objectForKey: @"Cities"]) {
                [citySearch objectForKey: @"Cities"];
                NSRange rangeSearch = [[citySearch valueForKey: @"Places"] rangeOfString: searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
                if (rangeSearch.location != NSNotFound) {
                    [filteredResult addObject: citySearch];
                }
            }
        }
    }

    if ([scope isEqualToString: @"Church"]) {
        for (NSDictionary *dictionarySearch in allRegion) {
            for (NSDictionary *citySearch in [dictionarySearch objectForKey: @"Cities"]) {
                for (NSDictionary *churchSearch in [citySearch objectForKey: @"Churches"]) {
                    [churchSearch objectForKey: @"Churches"];
                    NSRange rangeSearch = [[churchSearch valueForKey: @"Church"] rangeOfString: searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
                    if (rangeSearch.location != NSNotFound) {
                        [filteredResult addObject: churchSearch];
                    }
                }
            }
        }
    }
}
4

3 回答 3

0

我找到了我的问题的答案。

cellForRowAtIndexPath:

- (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];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        if (searchBar.selectedScopeButtonIndex == 0) {
            cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Places"];
        }

        else if (searchBar.selectedScopeButtonIndex == 1) {
            cell.textLabel.text = [[filteredResult objectAtIndex: indexPath.row] objectForKey: @"Church"];
        }
    }

    else {
        ...
    }

    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
    return cell;
}

searchBar.selectedScopeButtonIndex == 0对于范围城市和searchBar.selectedScopeButtonIndex == 1范围教堂

于 2013-01-06T08:33:26.913 回答
0

希望这个例子能帮助你。只需将您的“if”和“else if”内容放在链接示例中“searchResults”的“filterContentForSearchText”中,这肯定会起作用。

于 2012-12-29T06:34:24.383 回答
0

我认为使用带有搜索栏的 NSMutableArray 会对您有所帮助。您可以从链接中获取示例项目。https://github.com/AalokParikh/MyDemoApps

于 2012-12-29T06:34:48.620 回答