2010-11-12 49 views
0

我有一個mutableArray,它顯示了tableView中的文件列表。我已經將這些文件存儲在一個目錄中。iPhone - 文件列表的屬性

我使用NSfileManager類和attributesOfItemAtPath方法來檢索類似檔案大小的文件的某些屬性,文件類型..

在第二階段中我想具有顯示特定的文件時的性質的的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

NSString * parentDirectory = @「/ Users/akilan/Documents」; 將無法​​在真實設備上工作 – 2010-11-12 07:18:03

+0

Ya。我只是在模擬器中嘗試。我將在設備中使用時更改目錄。 – iOS 2010-11-12 07:23:29

+0

如果你接受解決你的問題的答案,這將是很好的。或者至少提供某種反饋,而不是僅僅不回答。 – 2010-11-14 21:02:26

回答

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]; 
} 
+0

沒有亞爾。它不工作.. – iOS 2010-11-15 09:26:03

+0

嗯。我試過在Xcode中,它確實有效。什麼不適合你? – 2010-11-15 09:29:10

+0

沒有什麼在tableView和控制檯中。 – iOS 2010-11-15 09:40:12

0

爲什麼不創建一個類「的FileDescriptor」具有以下屬性:

NSString *fileName; 
NSString *fileSize; 
NSString *fileType; 
NSDate *createdDate; 
NSDate *modifiedDate; 

當遍歷你的目錄,你爲每個文件創建這個類的一個實例,並將它存儲在一個數組(或者字典,如果你喜歡的話)。然後,你可以使用數組

一)爲您的UITableViewCells

b)顯示每個文件的詳細視圖單擊單元格時提供數據。

如果您想使用字典,而不是(我認爲這是少了很多「乾淨」),你可以這樣做是這樣的:

NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
    fileSize, @"fileSize", 
    fileName, @"fileName", nil]; 
+0

謝謝你。但是,你可以詳細敘述一個關於如何在這裏使用NSDictionary的源代碼嗎? – iOS 2010-11-12 08:25:49

+0

那麼,如果你想使用字典,而不是自定義對象(我認爲這將使代碼只有messier,你可以像這樣存儲這些值: NSDictionary * fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:fileSize,@「fileSize」, fileName,@「fileName」... etc ....,nil]; – 2010-11-12 08:31:25

+0

如果你想使用字典而不是數組,你把描述你的文件的對象放入一個字典中,例如絕對文件路徑爲當你檢索它們以顯示在你的表中時,你可以在你的cellForRowAtIndexPath方法中使用FileDescriptor * aFileDescriptor = [[yourDictionary allValues] objectAtIndex:indexPath.row] – 2010-11-12 08:33:40

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); 
    } 
} 
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 
+0

1.我沒有看到你在任何地方設置所有這些變量。 2.如果你使用[NSDictionary dictionaryWithObjectsAndKeys:],你需要在冒號後面指定一個對象,然後是一個鍵,然後是一個對象,然後是一個鍵等,然後是零。目前,您正在使用密鑰(NSString *)directoryContent存儲filesName(這是nil,因爲您沒有將它分配給任何人)。這是沒有意義的。 – 2010-11-12 12:52:22