ios - 在 UITableView 中列出目录的内容

标签 ios uitableview nsarray nsfilemanager

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

- (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] 但我得到了 objectAtIndex: 错误。有人有什么想法吗?

最佳答案

我其实很喜欢在这里提问,因为不到 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。现在修剪文件扩展名!

关于ios - 在 UITableView 中列出目录的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1740311/

相关文章:

iphone - UITableViewCell 独有的 UISwitch

ios - 在 UITableView 中显示文件的 NSArray - iOS 5

iOS:根据 View 位置动态更改 UILabel 对齐方式

ios - 如何以编程方式检测 iPhone 中的 EDGE 网络或不良网络?

ios - 视网膜显示屏上的高清和标清图形

ios - 在启用分页的情况下将 TableView 添加到 ScrollView 。

ios - 取消选择行的适当位置

ios sqlite 返回数据无法显示

swift - 为什么我收到错误 : "Class is not key-value coding compliant for key major" even though major is a property of the class?

ios - enumerateObjectsUsingBlock - 在 Swift 中转换参数