-1

我正在我的应用程序中实现音频播放器。我有一个 UITableView,我在其中显示了我的音轨列表。

音频列表被添加到一个数组中

fileArray = [[NSMutableArray alloc] initWithObjects:
                 [[NSBundle mainBundle] pathForResource:@"A Million Years" ofType:@"mp3"],
                 [[NSBundle mainBundle] pathForResource:@"2 Million Years" ofType:@"mp3"],...

在我的 cellForRowAtIndexPath 我正在做

cell.textLabel.text = [[NSString stringWithFormat:@"%@", [self.fileArray objectAtIndex:indexPath.row]] stringByDeletingPathExtension];

但问题是它仍然向我展示了一个完整的扩展名“/Users/....”。我只希望显示文件名(“一百万年”、“两百万年”)。

我究竟做错了什么?我一直在寻找答案,但从未得到答案(可能是由于措辞错误)

非常感谢大家。:)

4

2 回答 2

1

lastPathComponent如果你想要它的最后一部分,你应该使用它。所以:

cell.textLabel.text = [[[self.fileArray objectAtIndex:indexPath.row] lastPathComponent] stringByDeletingPathExtension];
于 2012-10-17T15:50:49.003 回答
0

那不是扩展名。它被称为“路径”。(在尝试对其进行编程之前,至少让自己在使用计算设备方面达到可接受的知识水平会很好......)

我建议你使用

[somePath lastPathComponent]

而是为了仅在没有包含目录的情况下获取文件名。

此外,调用[NSString stringWithFormat:]完全是多余的,浪费内存和处理器时间(这是不可接受的,尤其是在嵌入式设备上)。

于 2012-10-17T15:52:43.320 回答