如何在xib中的tableview中实现SearchBar和搜索显示控制器 [英] How to implement SearchBar and search display controller in tableview in xib

查看:85
本文介绍了如何在xib中的tableview中实现SearchBar和搜索显示控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有dictionaries的可变数组。我在表视图中显示该数组。

I have mutable array with dictionaries.I`am displaying that array in table view.

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

Now i want to implement search and display controller to table view. How?

任何建议或代码..

这里我的数组我显示名称键uitableview按字母顺序排列。

Here my array i`am displaying "name" key in uitableview as alphabetically order.

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


推荐答案

以下是示例代码

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
}



委托方法



Delegate Methods

-(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包含[c]%@ ,textrestaurantName是需要过滤的属性名称。如果您的数据源数组中只有NSString,则可以使用类似@SELF contains [c]%@,text

In NSPredicate condition "@"SELF.restaurantName contains[c] %@",text " restaurantName is a property name which needs to filtered against. If you have only NSString in your datasource array, you can use like @"SELF contains[c] %@",text

一旦过滤完成,那么你需要相应地实现你的tableview委托。像这样的东西

Once the filter is done, then you need to implement your tableview delegate accordingly. Something like this

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

    }

}

比较tableview无论是过滤的tableview还是原始的tableview,并相应地为tableview设置委托和数据源。请注意,searchDisplayController是UIViewcontroller的可用属性,我们可以用它来显示过滤结果。

compare the tableview whether it is filtered tableview or original tableview and set the delegate and datasource for tableview accordingly.Please note, searchDisplayController is available property for UIViewcontroller and we can just use it to display filtered result.

要使上述代码生效,如果您在XIB或故事板中使用它,则需要使用搜索栏和搜索显示对象

For above code to work, you need to use "Search Bar and Search Display" object if you are using it in a XIB or storyboard

这篇关于如何在xib中的tableview中实现SearchBar和搜索显示控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆