我试图在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:错误。有人有什么想法吗?
答案 0 :(得分:1)
我真的喜欢在这里提问,因为在不到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。现在修剪文件扩展名!
答案 1 :(得分:1)
你应该删除NSString,NSMutableArray和for循环..最终的代码应该是这样的:
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:@"/Test"];
cell.textLabel.text = [fileList objectAtIndex:indexPath.row];
return cell;
顺便说一句,这个fileList和manager为每个单元重复创建..所以最好把它作为UITableViewController的全局变量,只分配1
答案 2 :(得分:0)
你的for循环只是迭代文件并将文件设置为当前路径。所以在循环结束时,theFiles将只是集合中的最后一个字符串。
尝试类似:
cell.textLabel.text = [fileList objectAtIndex:indexPath.row];