0

以下に、アプリケーションが idevice に保存されているドキュメント ディレクトリからファイルを取得するコードをいくつか示します。次に、mp4 であるファイルへのパスを正常に取得します。これは、保存しているものだからです。UiTableViewI have createdのセルにファイル名を表示することでこれを行います。ただし、コードは 1 つのセルに 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 = [filePathsArray[indexPath.row] lastPathComponent];
    return cell;
}
4

3 に答える 3

3
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(!filePathsArray)
    {
        if ([filePathsArray count] > 0)
            return [filePathsArray count];
        else
            return 1;
    }

    return 1;
}

読むべき:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [filePathsArray count];
}
于 2013-07-01T21:18:04.190 に答える