我有我需要編寫搜索機制的UIPicker。提供此選取器的數據是一串字符串。當用戶輸入搜索字符串時(我有一個處理該字符串的UISearchBar)。我想查找包含搜索項的所有字符串。從下面的例子看來,我需要編寫一個謂詞,然後過濾數組。我現在遇到的問題是我不知道如何向用戶顯示搜索結果。我嘗試將結果集分配回原始數組,但這不正確。有人可能會提出一種實現用代碼示例搜索UIPicker的技術嗎?iPhone SDK:如何在UIPicker中實現搜索?
NSMutableArray* names = [NSMutableArray arrayWithObjects:@"Andy", @"Bart", @"Bob", nil];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'b'"];
NSArray* namesStartingWithB = [names filteredArrayUsingPredicate: predicate];
在此先感謝。
謝謝你的例子,我不得不稍微修改它,因爲我使用的是NSMutableArray \t NSPredicate * predicate = [NSPredicate predicateWithFormat:@ 「SELF beginswith並[c] 'B'」]; \t [arrayCountryChoices filterUsingPredicate:謂詞]; \t [pickerCountry reloadComponent:0]; \t 這工作正常我唯一曲。 estion是如何在過濾器後恢復原始數據集? – user265550 2010-02-04 15:50:06
在我的示例中,數據是原始數據集,searchResults是已過濾的數據集。恢復原來我會這樣做:self.searchResults = [NSArray arrayWithArray:data]; – 2010-02-04 16:02:45