參考此問題 Get all file names starting with a prefix from Resource folder 這裏它表示單詞'前綴'。但在我的應用程序中,我需要從文本字段中給出的單詞開始的所有文件名稱到表格中。我怎樣才能實現呢?我嘗試了「%@」,然後提供了textfield內容。但它不工作。但是,如果我給「h」之類的東西,它會顯示所有以'h'開頭的文件名。請幫忙回覆。獲取以特定單詞開頭的txt文件名稱
-(IBAction)Do_Search:(id)sender
{
files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
[[NSBundle mainBundle] bundlePath] error:nil];
search_results_array = [files filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'h'"]];
NSLog(@"%@", search_results_array);
int search_res_count=[search_results_array count];
NSLog(@"Array has %d results...",search_res_count);
[search_results_array retain];
[Result_table reloadData];
//till this part it works fine. I got all file names starting with 'h' in table. now what i need is i should get all file names starting with the word given in the search bok
NSString *Search_string=Search_song.text;
NSString *filePath = [[NSBundle mainBundle] pathForResource:Search_string ofType:@"txt"];
if (filePath) {
NSString *myText = [NSString stringWithContentsOfFile:filePath];
if (myText) {
song_lyrics.text= myText;
//what i did here is if i give a complete file name in the search field (textfield). i am getting the complete file contents inside a text view
}
}
}
我知道代碼現在沒有正確的順序。我試圖用這種自上而下和自下而上的方法..
你可以給你的代碼,你試圖實現這個?給出了代碼 – RaysonK 2012-01-31 06:25:31
。抱歉,我試圖將數據加載到表格中。 – priya 2012-02-01 07:00:19