【问题标题】:Objective-C: How to list files from documents directory into a UITableView?Objective-C:如何将文档目录中的文件列出到 UITableView 中?
【发布时间】:2013-06-30 17:04:19
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    在您的代码中,您将数组填充到cellForRowAtIndexPath:,显然

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

    cellForRowAtIndexPath 之前调用。所以你需要在重新加载表视图之前初始化数组的内容。将以下几行放入 ViewDidLoad 或 viewWillAppear 方法中:

    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,否则如果逻辑未正确实现,可能会导致不一致。

    【讨论】:

    • 在没有文件的情况下,numberOfRowsInSection:为什么要返回1
    • @maddy :在该行中,您可以显示如下消息:“未找到记录”或任何适当的消息,以免用户感到困惑。
    • @maddy : 确保您将该单元格的 userInteractionEnabled 设置为 No
    • 为什么?无需禁用交互。
    • 如果您要发布一个在没有数据时显示 1 行的答案,您还需要解释原因,而不仅仅是在稍后的评论中。照原样令人困惑。
    【解决方案2】:

    tableView:numberOfRowsInSection: 被调用时,filePathsArray 为 nil,所以该方法返回 0。您的代码基本上说“我的 tableView 中没有行”。
    如果您的 tableView 没有任何行,tableView 不会要求单元格。所以你填充数组的方法永远不会被调用。

    将以下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];
    

    【讨论】:

      【解决方案3】:
      -(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];
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-12
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多