【问题标题】:searchDisplayController not updating UITableVIewsearchDisplayController 不更新 UITableVIew
【发布时间】:2015-07-13 21:12:22
【问题描述】:

我有一个带有搜索栏和搜索显示控制器的 UITableView。我的代码似乎是正确的,但是当我在搜索栏中输入时,我没有得到任何结果。 UITableView 保持不变。

我的 segue 是正确的,因为它仍然正确地将数据传递给 下一个VC就好了。

我们将不胜感激。

**注意:我意识到从 iOS8 开始不推荐使用 searchDisplayController,但为了了解,我想弄清楚这一点。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];

} else {
    return [bands count];

}
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [bands objectAtIndex:indexPath.row];
}

return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
}

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

return YES;
}

编辑

我注意到 searchBar 没有连接。当我连接它时,我注意到只要我点击它,搜索栏就会消失。看起来搜索栏在导航栏后面消失了。那是另一个问题。

我仍然无法正确更新表格。

来源:Source

编辑

顺便说一句,我知道为什么我的搜索栏消失了。当我应该将导航栏嵌入标签栏中时,我将标签栏嵌入到导航控制器中。

所以:标签栏 --> 导航控制器

添加以下代码阻止我的搜索栏向上移动并与状态栏重叠

self.definesPresentationContext = YES;

编辑

不知道为什么,但我注意到我的搜索栏/搜索控制器连接不正确。我将其删除并重新添加,一切都正确连接。具体来说:

searchDisplayController 未连接到乐队列表。

【问题讨论】:

    标签: ios objective-c uitableview uisearchdisplaycontroller


    【解决方案1】:

    是否调用了这个方法,即searchDisplayController的委托方法---

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

    以及你是否在这一行数据

     searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
    

    【讨论】:

      【解决方案2】:

      替换下面的方法。可能对你有帮助

      - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
      NSPredicate *resultPredicate = [NSPredicate
                                      predicateWithFormat:@"SELF contains[cd] %@",
                                      searchText];
      
      searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
      [self.tableView reloadData]; //add tableview reloadData
      }
      

      【讨论】:

        【解决方案3】:

        总而言之,我的代码是正确的。我的搜索栏/搜索控制器未正确接线。删除它并重新添加它可以正确连接所有内容。我的原始帖子中有更多信息。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-09
          • 1970-01-01
          相关资源
          最近更新 更多