简体   繁体   English

Objective-C:如何将文件目录中的文件列入UITableView?

[英]Objective-C: How to list files from documents directory into a UITableView?

Below I have some code that lists files from my documents directory into a UITableView . 下面我有一些代码将文档目录中的文件列入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;
    }

In your code, you are filling the array in cellForRowAtIndexPath: , and obviously 在您的代码中,您正在使用cellForRowAtIndexPath:填充数组,显然

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

is called before cellForRowAtIndexPath . 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: 将以下行放在ViewDidLoad或viewWillAppear中:

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

And you should do handling like: 你应该像以下一样处理:

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

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. 请注意,当没有文件或加载数据时,我返回1,您可以在该单元格中显示“正在加载数据...”或“找不到记录”之类的消息。 Make sure to set the userInteractionEnabled to NO for that cell else it may cause inconsistencies if the logic is not implemented properly. 确保将该单元的userInteractionEnabled设置为NO ,否则如果逻辑未正确实现,则可能导致不一致。

When tableView:numberOfRowsInSection: is called filePathsArray is nil, so 0 is returned from this method. tableView:numberOfRowsInSection:被调用时,filePathsArray为nil,因此从此方法返回0。 Your code basically says "there are no rows in my tableView". 你的代码基本上说“我的tableView中没有行”。
And the tableView does not ask for a cell if your tableView does not have any rows. 如果tableView没有任何行,则tableView不会要求单元格。 So your method that fills the array is never called. 因此,永远不会调用填充数组的方法。

Move the following 3 lines of code into - (void)viewDidLoad or - (void)viewWillAppear:(BOOL)animated 将以下3行代码移动到- (void)viewDidLoad- (void)viewWillAppear:(BOOL)animated

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

    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];

    for (count = 0; count < (int)[directoryContent count]; count++)
    {
        NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
    }
    return directoryContent;
}

-(void)viewDidLoad {
    [super viewDidLoad];

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM