3

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,
        }
    ],
  • I want to display array which has dictionaries –  Aug 09 '13 at 11:28
  • 2
    umm mods? That link is not a duplicate answer to this question. That link shows how to add search bar with storyboard and this users question is how to add to xib. They are different. – mafiOSo Oct 13 '13 at 22:50

2 Answers2

2

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

slysid
  • 5,236
  • 7
  • 36
  • 59
  • I have mutable array which has dictionaries. can u tell me how to display –  Aug 09 '13 at 12:21
  • Can you give a sample of your Array of Dictionary and on what condition you are trying for filtered out in dictionary? For example, in array of say first names, I can filter out using alphabets starting with A or B or C, something like a condition on which you want to filter. – slysid Aug 09 '13 at 12:46
  • Plz,check my edited question..i updated –  Aug 09 '13 at 12:56
  • https://github.com/slysid/iOS/tree/master/DictSearch Look for sample code for the data yo have provided. The search will filter for name. i.e f will filter out fish and fry in your filter result. – slysid Aug 09 '13 at 14:39
  • search is working properly...but cells is not displaying properly.it`s overlapping cell on cell. –  Aug 12 '13 at 06:02
  • Is overlapping occurs in sample project I have given? It looks fine for me. Moreover, the code is to explain and how to use search using array of dicts. Any overlap of cells, may be due frame size not set properly. – slysid Aug 12 '13 at 08:23
  • I implemented your logic in my project.When i start search in searchBar all cells of search result are overlapping each other. –  Aug 12 '13 at 08:42
  • Have you implemented any custom tableview cell font or text size in it? Try to implement - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and see if the tableview is search result, then return a higher row height value. – slysid Aug 12 '13 at 09:22