0

I have an array that contains a dictionary i have to implement the search function on this array and get it to display only the typed in text in the search bar ........................

i have tried this code

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0) {
        isFiltered = NO;
    }
    else {
        isFiltered = YES;

    FilteredData = [[NSMutableArray alloc]initWithArray:[ParsedData valueForKey:@"FolderName"]];       
    NSMutableArray *searchData = [NSMutableArray new];
    for(NSDictionary *dict in ParsedData){

        NSDictionary *tempDict= @{@"FolderName":dict[@"FolderName"],@"ID":dict[@"ID"]}              
        [searchData addObject:tempDict];

        SearchData = [[NSMutableArray alloc]initWithArray:searchData];
        NSLog(@"DATA %@",SearchData);

    }

    //Fast Enumeration

    for (NSString *fileName in FilteredData)
    {
        NSRange fileNameRange = [fileName rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (fileNameRange.location == NSNotFound) {

            [SearchData removeObject:[SearchData valueForKey:@"FolderName"]];

        }
    }

}

[listingSet reloadData];
}




 This is the data present in parsed data as below plz check out.parse data array is    getting data from an xml


 2013-05-02 15:14:19.935 DocumentManagement[423:207] PARSED DATA (
    {
    CreatedBy = 1;
    FolderName = Posteingang;
    ID = 13000;
    ParentID = 0;
},
  {
    CreatedBy = 1;
    FolderName = "my folder";
    ID = 13183;
    ParentID = 0;
 },
Vishal
  • 168
  • 1
  • 16
  • So you want to have only objects which matches the folderName in SearchData array, rite? – Anupdas May 02 '13 at 10:13
  • @Anupdas That is exactly what i want – Vishal May 02 '13 at 10:15
  • Can you provide the data contained in ParsedData? – Anupdas May 02 '13 at 10:24
  • If that is the parsedData structure, then my answer will hold good :) – Anupdas May 02 '13 at 10:48
  • @Anupdas it doesnot searching anything – Vishal May 02 '13 at 11:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29305/discussion-between-anupdas-and-rana) – Anupdas May 02 '13 at 11:23
  • searchData is the dataSource of your tableView rite, can you check by breakPoints if the code is getting executed? – Anupdas May 02 '13 at 11:35
  • ParsedData is the Data source for my tableview and Searchdata is the array which is the source for the new tableview that is formed after text is entered in the search bar – Vishal May 02 '13 at 11:37
  • After running my code, can you check if there is any data coming in searchData. You are only showing part of your code, you need to make the other changes. When you are changing the dataSource, reload to reflect in your other tableView. – Anupdas May 02 '13 at 11:41

1 Answers1

1

From what I understood you have an array of dictionaries stored in parseData array. You want to form an subArray of it matching the value FolderName with searchText.

You can do it using NSPredicate

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0) {
        isFiltered = NO;
    }
    else {
        isFiltered = YES;

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FolderName LIKE [cd] %@",searchText];
        searchData = [[parsedData filteredArrayUsingPredicate:predicate]mutableCopy];
    }

    [listingSet reloadData];
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60