0

tableView にファイルのリストを表示する mutableArray があります。これらのファイルをディレクトリに保存しました。

NSfileManagerクラスとメソッドを使用しattributesOfItemAtPathて、fileSize、fileType などのファイルのプロパティを取得しています。

第2段階では、tableViewに触れたときに特定のファイルのプロパティを表示するdetailViewコントローラーを用意しようとしています。

問題は、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];
    }
}

次のステップに進むのを手伝ってください..よろしくお願いします..

4

4 に答える 4

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];
}
于 2010-11-12T15:20:14.483 に答える
0

次のプロパティを持つクラス「FileDescriptor」を作成してみませんか。

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

ディレクトリを反復処理するときは、ファイルごとにこのクラスのインスタンスを作成し、それを配列 (または必要に応じて辞書) に格納します。次に、その配列を使用して

a) UITableViewCells にデータを提供する

b) セルをクリックすると、各ファイルの詳細ビューが表示されます。

代わりに辞書を使用したい場合は(これは「クリーン」ではないと思います)、次のようにできます。

NSDictionary *fileProperties = [NSDictionary dictionaryWithObjectsAndKeys:
  fileSize, @"fileSize", 
  fileName, @"fileName", nil]; 
于 2010-11-12T07:47:35.933 に答える
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);
    }
}
于 2010-11-12T12:00:10.400 に答える
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
于 2010-11-12T12:04:37.873 に答える