【问题标题】:Why doesn't my search table view appear when beginning search?为什么开始搜索时没有出现我的搜索表视图?
【发布时间】:2012-03-24 22:26:01
【问题描述】:

我已将 UISearchDisplayController 与搜索栏连接到我的应用程序中。我已经这样初始化了:

self.filteredObjectArray = [NSMutableArray arrayWithCapacity:[self.objectArray count]];

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
searchBar.delegate = self;
[self.view addSubview:searchBar];

UISearchDisplayController *searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplay.delegate = self;
searchDisplay.searchResultsTableView.delegate = self;
searchDisplay.searchResultsDataSource = self;
searchDisplay.searchResultsDelegate = self;

这与我在其他示例中看到的差不多。我还实现了适当的委托方法,重要的 filterContentForSearchText 和 shouldReload* 方法如下所示:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{   
    [self.filteredObjectArray removeAllObjects]; // First clear the filtered array.

    for (XRay *xray in objectArray) {
        NSString *combinedLabel = [self combinedLabel:xray];
        if ([combinedLabel rangeOfString:searchText options:(NSCaseInsensitiveSearch)].location != NSNotFound) {
            [self.filteredObjectArray addObject:xray];
        }
    }
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
     [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
 [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    return YES;
}

但是,当我给搜索文本框提供焦点或输入任何字符时,我的搜索不起作用。您还会注意到灰色视图没有出现在我的常规表格视图上。

编辑 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"XraySearchChoice";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    XRay *xray = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        xray = [self.filteredObjectArray objectAtIndex:indexPath.row];

    } else {
        xray = [self.objectArray objectAtIndex:indexPath.row];
    }

    cell.textLabel.text = [self combinedLabel:xray];

    return cell;
}

【问题讨论】:

    标签: iphone ios ios5 uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我知道出了什么问题,说实话我有点惊讶。一旦我在 SearchTableViewController 上设置了一个属性: @property (nonatomic, strong) UISearchDisplayController *searchDisplay;

    并更新了我的代码以使用该代码,一切都从那里开始。这是我使用新属性的初始化代码。

    - (void)viewDidLoad
    {
        self.filteredObjectArray = [NSMutableArray arrayWithCapacity:[self.objectArray count]];
    
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
        searchBar.delegate = self;
        self.tableView.tableHeaderView = searchBar;
    
        self.searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    
        self.searchDisplay.delegate = self;
        self.searchDisplay.searchResultsDataSource = self;
        self.searchDisplay.searchResultsDelegate = self;
    }
    

    【讨论】:

    • 您似乎在使用 ARC。在您将其声明为属性之前,它的释放时间比您预期的要早。您可以将其分配给 _searchDisplayController 实例变量或像您所做的那样声明一个属性。不要忘记在您的 viewDidUnload 中将其设置为 nil。
    【解决方案2】:
    Try with below code it will help you
    
     self.filteredObjectArray = [NSMutableArray arrayWithCapacity:[self.objectArray count]];
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];
        searchBar.delegate = self;
        //[self.view addSubview:searchBar];
        self.theTableView.tableHeaderView = searchBar;//theTableView your tableview object
    
        UISearchDisplayController *searchDisplay = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
        self.searchController = searchDisplay;
        [searchBar release];
        searchDisplay.delegate = self;
        searchDisplay.searchResultsDataSource = self;
        searchDisplay.searchResultsDelegate = self;
    

    【讨论】:

    • 很遗憾,这些更改没有帮助。
    • 在 cellForRowAtIndexPath 中遇到了你写的内容?
    猜你喜欢
    • 2016-11-18
    • 2020-07-09
    • 2012-06-29
    • 2020-04-10
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多