Objective-C:如何在单个UITableViewCells中列出文件?

时间:2013-07-01 21:08:53

标签: ios objective-c

Bellow我有一些代码可以从我的应用程序存储在idevice上的文档目录中检索文件。然后它成功检索到该文件的路径,这是一个mp4,因为这是我正在存储的。这是通过在我创建的UiTableView的单元格中显示文件名来实现的。但是,代码仅在一个单元格中显示一个文件。但是我想在自己的单元格中单独列出多个文件,因此最终用户可以根据他们想要的视频文件单独选择该单元格。

以下是检索并显示文件的文件目录但未在表格中列出多个文件的代码:

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

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:1)

在正常情况下,您应该从[filePathsArray count];方法返回numberOfRowsInSection。现在你在所有情况下实际上都从该方法返回1。我不确定您在numberOfRowsInSection方法的顶部使用逻辑确切地尝试了什么,但我认为您应该首先用{{替换方法的整个主体1}}。

还有一件事:你在那里有评论"如果数据加载已经完成,"但是你发布的代码中的所有工作都是在主线程上同步进行的。在您的return [filePathsArray count];方法完成后,您的viewDidLoad已初始化。您不需要做任何棘手的事情来解决filePathsArray仍然为零的情况,除非您在视图控制器的视图实际存在之前担心表视图加载数据加载。但那会非常奇怪。

答案 2 :(得分:0)

为什么首先在viewDidLoad下然后在tableview下反复初始化filePathsArray。

这就是我编写代码的方式。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

//initializing array and getting values the first time when page loads.
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];


}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

//return whatever the count of this array is. no need to do any if! checks  
return [filePathsArray count];
}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
    }

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