2013-05-02 64 views
0

我有一個數組,包含一個字典,我必須在這個數組上實現搜索功能,並讓它只顯示在搜索欄中輸入的文本...... ..................包含NSDictionary的數組的快速枚舉循環

我已經嘗試過這種代碼

-(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; 
}, 
+0

所以,你要做到這一點有僅在SearchData陣列,儀式的FOLDERNAME匹配的對象? – Anupdas 2013-05-02 10:13:49

+0

@Anupdas這正是我想要的 – Vishal 2013-05-02 10:15:20

+0

你能提供包含在ParsedData中的數據嗎? – Anupdas 2013-05-02 10:24:54

回答

1

從我瞭解你存儲在parseData數組字典的數組。你想要形成一個與searchText匹配值FolderName的子數組。

可以使用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]; 
}