代码之家  ›  专栏  ›  技术社区  ›  WrightsCS

列出UITableView中目录的内容

  •  1
  • WrightsCS  · 技术社区  · 16 年前

    我试图在TableView中列出铃声目录的内容,但是,我只获取目录中所有单元格中的最后一个文件,而不是每个单元格中的文件。这是我的代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        Profile_ManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            cell.hidesAccessoryWhenEditing = YES;
        }
    
        cell.accessoryType = UITableViewCellAccessoryNone;
        //cell.textLabel.text = @"No Ringtones";
        //cell.textLabel.text = @"Test";
    
        NSString *theFiles;
        NSFileManager *manager = [NSFileManager defaultManager];
        NSArray *fileList = [manager directoryContentsAtPath:@"/Test"];
        for (NSString *s in fileList){
            theFiles = s;
        }
        cell.textLabel.text = theFiles;
    
        return cell;
    }
    

    当我使用的时候,它加载的很好,没有错误 NSLog 它列出了目录中的所有文件。我甚至尝试过 [s objectAtIndex:indexPath.row] 但我得到 对象索引: 错误。有人有什么想法吗?

    3 回复  |  直到 14 年前
        1
  •  0
  •   Alex Deem    16 年前

    for循环只是迭代文件并将文件设置为当前路径。所以在循环结束时,这些文件将只是集合中的最后一个字符串。

    尝试以下方法:

    cell.textLabel.text = [fileList objectAtIndex:indexPath.row];
    
        2
  •  1
  •   WrightsCS    14 年前

    我很喜欢在这里提问,因为不到10分钟,我就回答了我自己的问题!

    这就是我如何使用上述代码的方法:

    NSMutableArray *theFiles;
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager directoryContentsAtPath:@"/Test"];
    for (NSString *s in fileList){
        theFiles = fileList;
    }
    cell.textLabel.text = [theFiles objectAtIndex:indexPath.row];
    return cell;
    

    我刚将nsstring设置为nsmutableArray,这使我可以使用objectatindex。现在修剪文件扩展名!

        3
  •  1
  •   Plato    14 年前

    您应该删除nsstring、nsmutableArray和for循环。最终代码应该如下所示:

    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager directoryContentsAtPath:@"/Test"];
    cell.textLabel.text = [fileList objectAtIndex:indexPath.row];
    return cell;
    

    顺便说一句,这个文件列表和管理器为每个单元重复创建。因此,最好将其设置为uiTableViewController的全局变量,并只分配1

    推荐文章