2009-11-16 90 views
1

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

- (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

也许你应该在头文件中创建数组,这样你就可以访问主文件中任何地方的文件。如果你不这样做,那么除非你已经预先设置了这个数字,但是如果你有人添加文件或者删除它,那么你怎样才能设置表格项的数量,这将更容易得到目录中项目的数量。 – Maximilian 2011-10-01 18:00:23

回答

0

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

试着这么做:

cell.textLabel.text = [fileList objectAtIndex:indexPath.row]; 
+0

对于这个答案你是半正确的,实际上不正确的是允许objectAtIndex的NSMutableArray。 – WrightsCS 2009-11-16 06:02:56

+0

objectAtIndex在NSArray上提供,但它不是吗? – 2009-11-16 06:28:02

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。现在修剪文件扩展名!

+0

你确实意识到你在这里多次复制数组,这是非常低效的。你不需要for循环,我怀疑你需要创建一个可变的数组副本。 – 2009-11-16 06:26:52

1

您应该删除的NSString,NSMutableArray里和循环..最终代码应该是这样的:

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

顺便说一句,这个的文件列表并反复经理创建的每个细胞。所以最好是使它一个UITableViewController的全局变量,并且只分配1个