我试图在 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/