【发布时间】:2012-01-31 06:16:31
【问题描述】:
参考这个问题 Get all file names starting with a prefix from Resource folder 这里它说“前缀”这个词。但是在我的应用程序中,我需要从文本字段中给出的单词开始的所有文件名到表格中。我该如何实施?我尝试使用“%@”,然后给出了文本字段内容。但它不起作用。但是,如果我给出类似“h”的内容,它会显示所有以“h”开头的文件名。请帮忙回复。
-(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
}
}
}
我知道代码现在的顺序不正确。我正在尝试这种自上而下和自下而上的方法..
【问题讨论】:
-
你能给出你试图实现这个的代码吗?
-
给出代码。抱歉,我试图将数据加载到表格中。
标签: objective-c xcode textfield