iPhone-文件列表的属性

iPhone-文件列表的属性,iphone,uitableview,detailsview,nsfilemanager,Iphone,Uitableview,Detailsview,Nsfilemanager,我有一个可变数组,它在tableView中显示文件列表。我已将这些文件存储在一个目录中 我正在使用NSfileManager类和attributesFiteMatPath方法来检索文件的一些属性,如fileSize、fileType 在第二个阶段中,我尝试使用detailView控制器,当在tableView中触摸时,它会显示特定文件的属性 问题是我不知道如何将fileSize和fileType等属性分别绑定到NSDictionary并使其显示在特定文件的detailView中 下面是我列出文件

我有一个可变数组,它在tableView中显示文件列表。我已将这些文件存储在一个目录中

我正在使用
NSfileManager
类和
attributesFiteMatPath
方法来检索文件的一些属性,如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];
    }
}
请帮我进行下一步。。提前感谢。

为什么不创建具有以下属性的类“FileDescriptor”:

NSString *fileName;
NSString *fileSize;
NSString *fileType;
NSDate *createdDate;
NSDate *modifiedDate;
在目录上迭代时,为每个文件创建一个此类实例,并将其存储在数组(或者字典,如果愿意的话)中。然后可以使用该数组

a) 为UITableViewCells提供数据

b) 单击单元格时显示每个文件的详细视图

如果你想改用字典(我认为字典不太干净),你可以这样做:

NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  fileSize, @"fileSize", 
  fileName, @"fileName", nil]; 
- (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];
}
#导入
@接口MyFileList:UIViewController{
NSMutableArray*目录内容;
IBUITableView*文件表;
IBN编号*文件大小;
IBMOutlet NSString*文件名;
IBNSString*文件类型;
IBMOutlet NSString*文件路径;
IBNSDATE*创建日期;
IBNSDATE*修改日期;
NSMutableDictionary*文件目录;
}
@属性(非原子,保留)NSMutableArray*directoryContent;
@属性(非原子,保留)IBUITableView*文件表;
@属性(非原子,保留)IBN编号*文件大小;
@属性(非原子,保留)IBMOutlet NSString*FileName;
@属性(非原子,保留)IBMOutlet NSString*文件类型;
@属性(非原子,保留)IBMOutlet NSString*FilePath;
@属性(非原子,保留)ibnsdate*createdDate;
@属性(非原子,保留)IBNSDATE*modifiedDate;
@属性(非原子,保留)NSMutableDictionary*FileDirectory;
-(作废)清单文件;
-(void)willProcessPath;
@结束

尝试以下方法:

NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  fileSize, @"fileSize", 
  fileName, @"fileName", nil]; 
- (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];
}

NSString*parentDirectory=@“/Users/akilan/Documents”;不会在真正的设备上工作的。我只是在模拟器里试一下。在设备中使用时,我将更改目录。如果您接受的答案能够解决您的问题,那就太好了。或者至少提供一些反馈,而不是不再回答。但是你能用源代码详细说明一下如何使用NSDictionary吗?好吧,如果你想使用dictionary而不是自定义对象(我认为这会使代码更混乱),你可以像这样存储这些值:NSDictionary*fileProperties=[NSDictionary dictionary WithObjectsAndKeys:fileSize,@“fileSize”,fileName@“fileName”…等…,nil];如果要使用字典而不是数组,可以将描述文件的对象放入字典中,例如绝对文件路径作为键。当检索它们以显示在表中时,可以使用FileDescriptor*aFileDescriptor=[[yourDictionary allValues]ObjectAtIndexath.row]在你的CellForRowatineXpath方法中。我想使用dictionary而不是你说的自定义对象。你能给我发送一个使用该方法的参考吗?或者任何关于如何启动的源代码吗?嗨,我尝试过了。但它不起作用。控制台显示(null)当我运行它时。如果可能的话,请在我的代码中使用您的方法并发布它。1.我看不到您在任何地方设置所有这些变量。2.如果您使用[NSDictionary Dictionary WithObjectsAndKeys:],您需要在冒号后指定一个对象,然后是键,然后是对象,然后是键等,然后是nil。当前您正在存储文件名(它是nil,因为您没有分配它)和键(NSString*)directoryContent。这没有任何意义。嗯。我在Xcode中尝试过,它确实起作用。到底什么对您不起作用?tableView和控制台中没有任何内容。documents文件夹中有文档吗?如果没有,您将不会得到任何输出。视图中没有任何输出,只有控制台中。请确保将一些文档放入该文件夹中t文件夹…如果您想知道计算机上文件夹的路径,只需在NSLog某处->NSLog(@“%@”,parentDirectory)中放入一个NSLog;现在我在路径中添加了一些文件。但它显示了一个错误:由于未捕获的异常“NSInvalidArgumentException”,终止应用程序,原因:'-[\u NSCFDictionary isEqualToString:]:无法识别的选择器已发送到实例0x6a2cf40'嗯,那么您的代码中可能有错误。请将完整的xcode项目发布到某个位置,我将进行查看。