2012-01-31 59 views
1

參考此問題 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 
     } 
    } 
} 

我知道代碼現在沒有正確的順序。我試圖用這種自上而下和自下而上的方法..

+1

你可以給你的代碼,你試圖實現這個?給出了代碼 – RaysonK 2012-01-31 06:25:31

+0

。抱歉,我試圖將數據加載到表格中。 – priya 2012-02-01 07:00:19

回答

0

它是如此簡單....代替 'H' 我刪除了從那裏 '',並給%@即

search_results_array = [files filteredArrayUsingPredicate: 
         [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@",Search_string]]; 

那裏解決了我的問題...謝謝大家:)

+0

請務必接受您自己的回答 – RaysonK 2012-02-01 18:54:56

+0

我確定...我得到了結果... – priya 2012-02-02 03:45:00

+1

我的意思是點擊投票數 – RaysonK 2012-02-02 04:25:03

0

Pl。改變線

search_results_array = [files filteredArrayUsingPredicate: 
         [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] 'h'"]]; 

如下

search_results_array = [files filteredArrayUsingPredicate: 
         [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] '%@'",searchBoxText]]; 
+0

該數組是空的然後在這裏searchBoxText是Search_song爲我。 – priya 2012-02-01 08:04:02

相關問題