Objective-C:如何将文件目录中的文件列出到UITableView中? [英] Objective-C: How to list files from documents directory into a UITableView?

查看:45
本文介绍了Objective-C:如何将文件目录中的文件列出到UITableView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,我有一些代码将文件目录中的文件列出到 UITableView 中.但是,该代码无法正常工作,即使我在设备上进行了测试,即使documents目录中有文件,也没有列出所有内容,仅显示了一些空白单元格.这是我当前正在使用的代码:

Below I have some code that lists files from my documents directory into a UITableView. However, the code is not correctly working and once I test on my device even though there is files in the documents directory, nothing is listed all displayed just displaying some blank cells. Here is the code I am currently using:

NSArray *filePathsArray;

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    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"];
        }
        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;
    }

推荐答案

在您的代码中,您正在 cellForRowAtIndexPath:中填充数组,并且显然是

In your code, you are filling the array in cellForRowAtIndexPath:, and obviously

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分

.因此,您需要在重新加载表视图之前初始化数组的内容.将以下行放入ViewDidLoad或viewWillAppear方法中:

is called before cellForRowAtIndexPath. So you need to initialize the contents of the array before reloading table view. Either put the following lines in ViewDidLoad or viewWillAppear Methods:

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

您应该像这样进行处理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if(!isDataLoading)  // 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;
}

请注意,当没有文件或正在加载数据时,我将返回1,您可以在该单元格中显示一条消息,例如正在加载数据..."或未找到记录".请确保将该单元格的 userInteractionEnabled 设置为 NO ,否则,如果逻辑实现不正确,则可能导致不一致.

Note that I am returning a 1 when there are no files or when the data is loading, you can display a message like "Loading data..." or "No records found" in that cell. Make sure to set the userInteractionEnabled to NO for that cell else it may cause inconsistencies if the logic is not implemented properly.

这篇关于Objective-C:如何将文件目录中的文件列出到UITableView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆