2014-02-25 62 views
0

我想在我的應用程序中使用UISearchBar搜索圖像。我在tableViewController中有超過100個圖像,每個圖像都包含名稱和關鍵字(例如imageName = u1-1f.png,u1-2f.png,... Keyword = Bonds,Carbohydrates)。我想搜索關鍵詞「債券」,它會導致u1-1f.png等。我正在使用plist,並從plist獲得數據到數組,如何在iOS中搜索圖像

NSString *path = [[NSBundle mainBundle] pathForResource:@"keywords" ofType:@"plist"]; 
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
NSArray* allmyKeys = [myDictionary allValues]; 
NSLog(@"%@", allmyKeys); 

如何從搜索欄搜索圖像。

回答

0

嘗試像這樣...使用NSPRedicate

NSArray *array = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"filter string" forKey:@"email"]]; // key 
NSArray *filteredarray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(email == %@)", @"filter string"]]; 
0

如果您有沒有比我想它能夠更好地使用plist出現數組的內容,我想你正在使用的NSDictionary任何特殊的理由。

NSMutableArray *yourArray=[[NSMutableArray alloc]init]; 
NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSString *finalPath = [path stringByAppendingPathComponent:@"yourPlist.plist"]; 
NSMutableArray * plistArray = [NSMutableArray arrayWithContentsOfFile:finalPath]; 
for(int key=0; key!=[plistArray count]; key++){ 

    [yourArray addObject:[plistArray objectAtIndex:key ]]; 

} 

所以你有你的圖像在yourArray。

for(int i; i!=[yourArray count]; i++) 
    { 
if([yourTextField.text isEqualString:[yourArray objectAtIndex:i]]){ 
NSString *filePath = [[NSBundle mainBundle] pathForResource:[yourArray objectAtIndex:i] ofType:@"png"]; 
UIImage *image=[[UIImage alloc] initWithContentsOfFile:filePath]; 
} 
} 

你可以把你的圖片寫在你的文本框中。

順便說一句,我沒有檢查這個代碼,但它可能會工作。

我希望這些代碼能幫助你解決你的問題。 祝你好運