【问题标题】:How to implement SearchBar and search display controller in tableview in xib [duplicate]如何在xib的tableview中实现SearchBar和搜索显示控制器[重复]
【发布时间】:2013-08-09 11:01:10
【问题描述】:

我有带有字典的可变数组。我正在表格视图中显示该数组。

现在我想在表格视图中实现搜索和显示控制器。怎么样?

任何建议或代码..

在这里,我的数组按字母顺序在 uitableview 中显示“名称”键。

[
        {
            "name": "Fish",
            "description": "sdhshs",
            "colorCode": null,
        },
        {
            "name": "fry",
            "description": "sdhshs",
            "colorCode": null,
        },
        {
            "name": "curry",
            "description": "sdhshs",
            "colorCode": null,
        }
    ],

【问题讨论】:

  • 我想显示有字典的数组
  • 嗯模组?该链接不是此问题的重复答案。该链接显示了如何使用情节提要添加搜索栏,而这个用户的问题是如何添加到 xib。它们是不同的。

标签: iphone ios uitableview uisearchbar uisearchdisplaycontroller


【解决方案1】:

这是一个示例代码

NSMutableArray *filteredResult; // this holds filtered data source
NSMutableArray *tableData; //this holds actual data source

-(void) filterForSearchText:(NSString *) text scope:(NSString *) scope
{
    [filteredResult removeAllObjects]; // clearing filter array
    NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.restaurantName contains[c] %@",text]; // Creating filter condition
    filteredResult = [NSMutableArray arrayWithArray:[tableData filteredArrayUsingPredicate:filterPredicate]]; // filtering result
}

委托方法

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

    return YES;
}

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

    return YES;
}

在 NSPredicate 条件 "@"SELF.restaurantName contains[c] %@",text " restaurantName 是需要过滤的属性名称。如果您的数据源数组中只有 NSString,您可以使用 @"SELF contains[c] %@",text

过滤完成后,您需要相应地实现您的 tableview 委托。像这样的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == [[self searchDisplayController] searchResultsTableView])
    {
        return [filteredResult count];
    }
    else
    {
        return [tableData count];

    }

}

比较tableview是过滤后的tableview还是原始tableview,并相应设置tableview的delegate和datasource。请注意,searchDisplayController是UIViewcontroller的可用属性,我们可以用它来显示过滤后的结果。

要使上述代码正常工作,如果您在 XIB 或情节提要中使用“搜索栏和搜索显示”对象,则需要使用它

【讨论】:

  • 我有一个包含字典的可变数组。你能告诉我如何显示
  • 你能提供一个字典数组的样本吗?你想在什么条件下过滤掉字典?例如,在说名字的数组中,我可以使用以 A 或 B 或 C 开头的字母进行过滤,就像您要过滤的条件一样。
  • 请检查我编辑的问题..我更新了
  • github.com/slysid/iOS/tree/master/DictSearch 查找您提供的数据的示例代码。搜索将过滤名称。即 f 将在您的过滤结果中过滤掉鱼和油炸物。
  • 搜索工作正常...但单元格显示不正确。单元格上的单元格重叠。
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多