我希望我的应用读取特定文件夹,并在UITableView中显示文件。
我有这个代码,它应该读取名为DOKS的文件夹,但是我无法获得要在textLabel上显示的文件标题。 有人能帮我吗?它会超级; - )
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"doks"];
NSArray *yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:&error];
recipes = [NSArray arrayWithObjects:yourFolderContents, nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [recipes count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:0)
您正在将数组中的数组设置为recipes
,而不是将其自身设置为内容数组。
recipes = [NSArray arrayWithObjects:yourFolderContents, nil];
相反,试试这个:
recipes = [NSArray arrayWithArray:yourFolderContents];
答案 1 :(得分:0)
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); //创建一个数组并存储我们搜索其中文档目录的结果
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"YOUR_FOLDER_NAME"];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:&error];
NSMutableArray *filenameArray = [NSMutableArray array];
//if you want to retrive any specific format files
for(NSString *filename in directoryContents) {
if ([[filename pathExtension] isEqualToString:@"txt"]) {
[filenameArray addObject:filename];
}
}
之后你会在filenameArray中找到所有文件名的列表。
看看它是否适合你。