0

Bellow i have some code that lists the path to the documents directory where my video files are saved. The code then lists that in a cell but it's the full path and only one cell is used. So what i want to do is first cut down the path and only list the file name of the file in the documents director and then have an individual cell for each file. Is that possible?

Here is the code i am using to list the path to the documents directory:

- (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;
}

Thanks in advance.

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 回答