2014-07-04 51 views
0

我在我的应用程序下面的搜索设置:UISearchDisplayController不更新搜索结果

- (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委托和数据源是在我的故事板中设置的。

任何想法为什么会发生这种情况?

UPDATE:

[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; 
} 
+0

你有你的控制器中的另一个表视图?你能告诉我们你的'cellForRowAtIndexPath','numberOfSection'和'numberOfRows'方法吗? – michaelsnowden

+0

更新了方法。 – 7c9d6b001a87e497d6b96fbd4c6fdf

回答

2

这是有问题的行:

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

编辑 退房this example

+0

谢谢,但这似乎没有改变任何东西:/ – 7c9d6b001a87e497d6b96fbd4c6fdf

+0

我已经在我的ViewDidLoad中使用resueidentifier(我的故事板中的一个)为prototypecell注册了一个TableViewCell类,这是否有所作为? – 7c9d6b001a87e497d6b96fbd4c6fdf

+0

@Seya我必须看到更多你的代码。我将立刻为你做一个示例搜索表视图类。当我在大约5分钟内发布它时,请看看它。 – michaelsnowden