【问题标题】:Display data from the documents folder into UITableView and go to the selection in Objective C将文档文件夹中的数据显示到 UITableView 并转到 Objective C 中的选择
【发布时间】:2012-04-14 03:45:18
【问题描述】:

我正在制作一个允许用户创建文件 (html) 并编辑/查看它们的应用程序。这些文件保存在应用程序的 Documents 文件夹中。我想知道是否有任何好的方法可以在 UITableView 中显示所有文件和文件夹。

另外,您如何将用户的选择传递给要阅读的文本视图?我知道如果函数可以加载文件,那么如何获取行 ID 并传递名称...

【问题讨论】:

    标签: objective-c ios save


    【解决方案1】:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *currentDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
    

    将在文档目录中为您提供一个文件名数组作为 NSString,然后您只需将其用作表格视图的数据。当一个文件被选中时,你可以使用NSMutableDatainitWithContentsOfFile方法和路径作为变量来获取一个NSMutableData对象传递给你的编辑器。

    【讨论】:

      【解决方案2】:

      如果您正在设置单元格文本标签,那么传递字符串是不费吹灰之力的。在 didSelectRowAtIndexPath 中,执行如下操作:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      //alloc and init view with custom init method that accepts NSString* argument  
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:"];
      NSString*pathToPass = [documentsDirectory stringByAppendingPathComponent:[self.tableView cellForRowAtIndexPath:indexPath].textLabel.text]; //pass this.
      
      }
      

      【讨论】:

      • 能否请您加入词典 (cd)。如果您这样做,您可以获得支票:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 2020-12-12
      • 1970-01-01
      相关资源
      最近更新 更多