How to implement SearchBar and search display controller in tableview in xib [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-01 22:59:17

问题


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?

Any suggestions or code..

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,
        }
    ],

回答1:


Here is a sample code

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

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

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

    }

}

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.

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




回答2:


Refer the following sample codes,

http://developer.apple.com/library/ios/samplecode/AdvancedTableSearch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40013493

http://developer.apple.com/library/ios/samplecode/TableSearch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007848

http://www.appcoda.com/how-to-add-search-bar-uitableview/

These examples may give you a better idea



来源:https://stackoverflow.com/questions/18145209/how-to-implement-searchbar-and-search-display-controller-in-tableview-in-xib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!