簡體   English   中英

UISearchDisplayController不更新搜索結果

[英]UISearchDisplayController does not update search results

我的應用程序中具有以下搜索設置:

- (UISearchBar*)searchBar
{

    if (!_searchBar) {

        _searchBar = [[UISearchBar alloc] init];
        _searchBar.delegate = self;
        _searchBar.placeholder = kSearchBarPlaceHolder;
    }
    return _searchBar;
}
- (UISearchDisplayController*)searchBarDisplayContr
{

    if (!_searchBarDisplayContr) {

        _searchBarDisplayContr = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
        _searchBarDisplayContr.delegate = self;
        _searchBarDisplayContr.searchResultsDataSource = self.tableView.dataSource;
        _searchBarDisplayContr.searchResultsDelegate = self.tableView.delegate;
        _searchBarDisplayContr.searchResultsTableView.backgroundColor = [UIColor clearColor];
        _searchBarDisplayContr.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    }

    return _searchBarDisplayContr;
}

- (NSMutableArray *)searchResults
{

    if (!_searchResults) {
        _searchResults = [NSMutableArray arrayWithCapacity:_restaurants.count];
    }

    return _searchResults;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

    [self.searchResults removeAllObjects];

    [self.restaurants enumerateObjectsUsingBlock:^(Restaurant *restaurant, NSUInteger idx, BOOL *stop) {
        if ([scope isEqualToString:@"All"] || [restaurant.name isEqualToString:scope]) {

            NSRange range = [restaurant.name rangeOfString:searchText
                                                   options:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)];


            if (range.length > 0) {
                [self.searchResults addObject:restaurant];
            }
       }

    }];



}

- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
    [self filterContentForSearchText:searchString
                               scope:@"All"];



    dispatch_async(dispatch_get_main_queue(), ^(void) {
        for (UIView *v in controller.searchResultsTableView.subviews) {
            if ([v isKindOfClass:[UILabel class]]) {
                ((UILabel *)v).text = kSearchResultsTableViewNoResultsLabel;
                ((UILabel *)v).font = [UIFont mediumFontOfSize:20.0f];
                ((UILabel *)v).textColor = [UIColor blackColor];
                break;
            }
        }
    });


    return YES;
}



- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchBarDisplayContr.searchBar text]
                               scope:@"All"];

    return YES;
}

但是由於某些原因,當我搜索searchResultsTableView時未更新/未調用cellForRowAtIndexPath。 tableView委托和DataSource已在我的情節提要中設置。

任何想法為什么會這樣?

更新:

 [self.tableView registerClass:[RestaurantsCell class] forCellReuseIdentifier:cellIdentifier];

    [self.searchBarDisplayContr.searchResultsTableView registerClass:[RestaurantsCell class] forCellReuseIdentifier:cellIdentifier];


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchBarDisplayContr.searchResultsTableView) {

        return [self.searchResults count];

    } else {       

           return [self.restaurants count];


    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    RestaurantsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    Restaurant *restaurant;

    if (tableView == self.searchBarDisplayContr.searchResultsTableView) {

        if (self.searchResults.count > 0) {

            restaurant = self.searchResults[indexPath.row];


        }


    } else {   

 if (self.restaurants.count > 0) {     

          restaurant = self.restaurants[indexPath.row];

}

    }


    if (restaurant) {

        cell.titleLabel.text = restaurant.name;
    }
    return cell;
}

這是違規行:

RestaurantsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

應該:

RestaurantsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

說明:

如果tableView原來是您的搜索結果表視圖,則嘗試使帶有標識符的單元出隊將不起作用,因為它沒有原型單元。 因此,您必須使用self.tableView

此外,您的代碼可以被清理很多:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{

    return (tableView == self.tableView) ? self.restaurants.count : self.searchResults.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    RestaurantsCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    Restaurant *restaurant = (tableView == self.tableView) ? self.restaurants[indexPath.row] ? self.searchResults[indexPath.row];
    }

    cell.titleLabel.text = restaurant.name;
    return cell;
}

編輯查看此示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM