【问题标题】:UISearchBar results not shownUISearchBar 结果未显示
【发布时间】:2013-07-03 16:08:34
【问题描述】:

我在我的应用程序中使用 UISearchBar。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{
    [self.filtered removeAllObjects];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.companyName contains[cd] %@",searchText];
    NSArray *tempArray = [ee filteredArrayUsingPredicate:predicate];
    filtered = [NSMutableArray arrayWithArray:tempArray];
}

我在上面的“过滤”数组上做了NSLog,它得到了正确的搜索结果。

我没有在表格视图中看到过滤后的值。如果我输入错误的搜索文本,我会看到“无结果”。但是对于正确的搜索文本,表格视图是空的。

谁能帮我做什么?

提前致谢!!

【问题讨论】:

  • 检查你的数组是否为空
  • 你是否重新加载了tableview。过滤后的数组是tableview的数据源吗?
  • 我的过滤数组不为空,我做了 NSLog 来查看过滤后的值。我在 cellForRowAtIndexPath 方法和 numberOfRowsInSection 中也看到了过滤后的数组值。
  • 我在 viewDidLoad 中只有一个重新加载调用。我不确定您所说的过滤数组是数据源是什么意思?我没有明确说它是数据源。如果我错过了,请告诉我该怎么做。谢谢。
  • 你能贴出cellForRowAtIndexPath代码吗?

标签: ios uisearchbar uisearchbardelegate


【解决方案1】:

请使用以下代码。并确保 self.filtered 是 yourtableview 的数据源。数据源表示数组 从 tableview 被填充

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
    {
        [self.filtered removeAllObjects];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.companyName contains[cd] %@",searchText];
        NSArray *tempArray = [ee filteredArrayUsingPredicate:predicate];
        filtered = [NSMutableArray arrayWithArray:tempArray];
   [yourtableview reloadData];
    }

【讨论】:

    【解决方案2】:
    Write the above code  then Reload the table in the source code 
    
    
       [tableView reloadData];
    

    【讨论】:

    • 我已经缩小了问题的范围。如果单元格是 UITableViewCell,则来自 SearchBar 的过滤器正在工作。我有一个自定义单元格,其中我已将 UITableViewCell 扩展到 ECell。使用它,Filterdata 不起作用。有什么想法吗?
    • 对象重载的表格视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2016-04-08
    • 2021-08-30
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多