我有一个mutableArray,它在tableView中显示一个文件列表。我已将这些文件存储在目录中。
我正在使用NSfileManager
类和attributesOfItemAtPath
方法来检索文件的某些属性,例如fileSize,fileType ..
在第二阶段,我正在尝试使用detailView控制器,该控制器在tableView中触摸时显示特定文件的属性。
问题是我不知道如何将这些属性(如fileSize和fileType)分别绑定到NSDictionary
,并使其显示在该特定文件的detailView中。
这是我列出文件和列出属性的源代码。
- (void)listFiles {
NSFileManager *fm =[NSFileManager defaultManager];
NSError *error = nil;
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
error = nil;
}
directoryContent = [[NSMutableArray alloc] init];
for (NSString *path in paths){
NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
[directoryContent addObject:documentsDirectory];
}
}
-(void) willProcessPath {
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
for (NSString *path in paths){
filesPath = [parentDirectory stringByAppendingPathComponent:path];
NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
filesSize = [attrDir objectForKey:NSFileSize];
filesName = (NSString *)directoryContent;
filesType = [path pathExtension];
createdDate = [attrDir objectForKey:NSFileCreationDate];
modifiedDate = [attrDir objectForKey:NSFileModificationDate];
}
}
请帮我继续下一步..提前致谢..
答案 0 :(得分:1)
尝试这样的事情:
- (void)viewDidLoad {
[super viewDidLoad];
[self listFiles];
}
- (void)listFiles {
NSFileManager *fm =[NSFileManager defaultManager];
NSError *error = nil;
NSString *parentDirectory = [self getDocumentsPath];
NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
error = nil;
}
NSMutableArray *array = [[NSMutableArray alloc] init];
self.directoryContent = array;
[array release];
for (NSString *path in paths){
NSString *fullPath = [self getPathToFileInDocumentsDirectory:path];
NSMutableDictionary *filePropertiesDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil]];
[filePropertiesDictionary setObject:fullPath forKey:@"fullPath"];
[filePropertiesDictionary setObject:path forKey:@"fileName"];
[directoryContent addObject:filePropertiesDictionary];
}
NSLog(@"%@", directoryContent);
}
- (NSString *)getDocumentsPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return documentsDirectoryPath;
}
- (NSString *)getPathToFileInDocumentsDirectory:(NSString *)fileName {
return [[self getDocumentsPath] stringByAppendingPathComponent:fileName];
}
答案 1 :(得分:0)
为什么不创建具有以下属性的类“FileDescriptor”:
NSString *fileName;
NSString *fileSize;
NSString *fileType;
NSDate *createdDate;
NSDate *modifiedDate;
在遍历目录时,为每个文件创建此类的实例,并将其存储在数组(或字典中,如果您愿意)中。然后你可以使用该数组
a)为您的UITableViewCells提供数据
b)单击单元格时显示每个文件的详细视图。
如果您想要使用字典(我认为不那么“干净”),您可以这样做:
NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:
fileSize, @"fileSize",
fileName, @"fileName", nil];
答案 2 :(得分:0)
- (void)listFiles {
NSFileManager *fm =[NSFileManager defaultManager];
NSError *error = nil;
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
error = nil;
}
directoryContent = [[NSMutableArray alloc] init];
for (NSString *path in paths){
NSString *documentsDirectory = [[path lastPathComponent] stringByDeletingPathExtension];
[directoryContent addObject:documentsDirectory];
}
}
-(void) willProcessPath {
NSString *parentDirectory = @"/Users/akilan/Documents";
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentDirectory error:nil];
for (NSString *path in paths){
filesPath = [parentDirectory stringByAppendingPathComponent:path];
NSDictionary *attrDir = [[NSFileManager defaultManager]attributesOfItemAtPath:filesPath error:nil];
filesDirectory = [NSDictionary dictionaryWithObjectsAndKeys:filesSize, [attrDir objectForKey:NSFileSize],
filesName, (NSString *)directoryContent, filesType, [path pathExtension],
createdDate, [attrDir objectForKey:NSFileCreationDate],
modifiedDate, [attrDir objectForKey:NSFileModificationDate], nil];
NSLog(@"%@", filesDirectory);
}
}
答案 3 :(得分:0)
#import <UIKit/UIKit.h>
@interface MyFilesList : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *directoryContent;
IBOutlet UITableView *filesTable;
IBOutlet NSNumber *filesSize;
IBOutlet NSString *filesName;
IBOutlet NSString *filesType;
IBOutlet NSString *filesPath;
IBOutlet NSDate *createdDate;
IBOutlet NSDate *modifiedDate;
NSMutableDictionary *filesDirectory;
}
@property (nonatomic, retain) NSMutableArray *directoryContent;
@property (nonatomic, retain) IBOutlet UITableView *filesTable;
@property (nonatomic, retain) IBOutlet NSNumber *filesSize;
@property (nonatomic, retain) IBOutlet NSString *filesName;
@property (nonatomic, retain) IBOutlet NSString *filesType;
@property (nonatomic, retain) IBOutlet NSString *filesPath;
@property (nonatomic, retain) IBOutlet NSDate *createdDate;
@property (nonatomic, retain) IBOutlet NSDate *modifiedDate;
@property (nonatomic, retain) NSMutableDictionary *filesDirectory;
- (void)listFiles;
- (void) willProcessPath;
@end