本文介绍了在UITableView - iOS 5中显示文件的NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户保存和打开文本文件。保存和打开文件是相当直接和容易的,但是我必须给用户一个简单的方法来选择要打开的文件。我决定使用 UITableView 来执行此操作。



当我的视图控制器中加载Ta​​bleView时,我的目标是使用名称填充TableView的文档文件夹(iOS应用程序沙箱的一部分)中的所有用户文本文件。



我有一个如何做到这一点的一般想法:



  1. 获取Documents文件夹的内容并将其放在数组中

      NSString * pathString = [[NSBundle mainBundle] pathForResource:@DocumentsofType:@txt]; 
    NSArray * fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error:nil];
    NSLog(@目录的内容:%@,fileList);

    但总是返回:(null)在输出窗口中。


  2. 将数组放在UITableView中



    我想我会使用 numberOfRowsInSection 方法来做到这一点。

  3. 最后,应用将会获取所选单元格的值,并使用该值打开文件。







我的主要问题是:如何将项目(特别是目录的内容)放入NSArray中,然后在UITableView中显示该数组?



任何帮助都非常感谢!

解决方案

您可以使用以下路径获取路径:

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

您可以使用以下方式获取目录的内容:

  NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString * documentsDirectory = [paths objectAtIndex:0];
NSArray * fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];对于在UITableView中显示NSArray,您应该查看Apple的UITableView数据源协议文档:







您使用cellForRowAtIndexPath方法实际填充表,这样可以起作用:

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

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@MyIdentifier];

if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@MyIdentifier] autorelease];
}

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [fileList objectAtIndex:indexPath.row]

return cell;
}


I have an app that allows users to save and open text files. Saving and opening files is fairly straightforward and easy, but I have to give the user an easy way to select a file to open. I decided to do this with a UITableView.

When the TableView loads in my view controller, my goal is to populate the TableView with the names of all of the user's text files from within the Documents folder (part of the iOS App Sandbox).

I have a general idea of how to do this:

  1. Get the contents of the Documents folder and place it in an array

    NSString *pathString = [[NSBundle mainBundle] pathForResource:@"Documents" ofType:@"txt"];
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathString error:nil];
    NSLog(@"Contents of directory: %@", fileList);
    

    But this always returns: (null) in the output window. What is the path for my app's document folder?

  2. Place the array in the UITableView

    I figured I'd use the numberOfRowsInSection method to do this. Is this the right place for performing this type of operation? Of should I use a different method?

  3. Finally, the app will get the value of the selected cell and use that value to open the file.


My main question here is: How can I place items (particularly the contents of directory) into an NSArray and then display that array in a UITableView?

Any help is much appreciated!

解决方案

You can get the path using:

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

You can get the contents of the directory using:

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

As for displaying an NSArray in a UITableView, you should review Apple's documentation on UITableView Data Source protocol:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

You use the cellForRowAtIndexPath method to actually populate the table, something like this would work:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"] autorelease];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [fileList objectAtIndex:indexPath.row]

    return cell;
}

这篇关于在UITableView - iOS 5中显示文件的NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:29