0

以下に、ビデオ ファイルが保存されているドキュメント ディレクトリへのパスをリストするコードをいくつか示します。コードはそれをセルにリストしますが、これは完全なパスであり、使用されるセルは 1 つだけです。だから私がやりたいことは、最初にパスを切り詰めて、ファイルのファイル名だけをドキュメントディレクターにリストしてから、各ファイルに個別のセルを用意することです。それは可能ですか?

ドキュメントディレクトリへのパスをリストするために使用しているコードは次のとおりです。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];


}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(!filePathsArray)  // if data loading has been completed, return the number of rows ELSE return 1
    {

        if ([filePathsArray count] > 0)
            return [filePathsArray count];
        else
            return 1;
    }

    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    cell.textLabel.text = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
    return cell;
}

前もって感謝します。

4

2 に答える 2

0

現在のように、メソッドが呼び出されるたびにファイル パスviewDidLoad をロードしているため、非効率的で不要です。tableView:cellForRowAtIndexPath:

いずれにせよ、次のようなものが必要です。

cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];

あなたの要件を正しく理解している場合。

于 2013-07-01T15:33:38.267 に答える
0

これを使って:

NSFileManager *fm [NSFileManager defaultManager];
NSArray *documentsDirectoryContents = [[fm contentsOfDirectoryAtPath:documentDirectory error:nil] mutableCopy];

それは私にとってはうまくいき、ドキュメントディレクトリ内のファイルのファイル名のみを返します(フルパスではありません)。次に、その配列をテーブルのデータ ソースとして使用します。したがって、各セルのテキストを対応するファイル名に設定します。

さらに、NSBum が言うように、ドキュメント ディレクトリの内容を更新する場合、つまり、テーブルを更新する場合にのみ、ファイル名を取得する必要があります。

于 2013-07-01T15:34:13.557 に答える