0

集合视图单元格中的音频文件顺序错误。在第一个单元格中它不播放任何内容,在第二个单元格中播放第一个音频文件,在第三个单元格中播放第二个音频文件,在第四个单元格中播放第三个音频文件,当再次单击第一个单元格时它播放第四个音频文件。

这是为什么。我在代码中做错了什么。

这是我的代码

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{


audioArray =[[NSArray alloc] initWithObjects:@"0", @"1", @"2", @"3", nil];

NSString *filePath = [audioArray objectAtIndex:indexPath.row];

NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:audioToLoad ofType: @"mp3"];

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: audioFilePath];

audioPlayer = [[AVAudioPlayer alloc]
               initWithContentsOfURL:fileURL error:nil];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{

// custom UICollectionViewCell, hold an image and its label

Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];

// make the cell's title the actual NSIndexPath value

cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];

// load the image for this cell

NSString *imageToLoad = [NSString stringWithFormat:@"%d.jpg", indexPath.row];

cell.image.image = [UIImage imageNamed:imageToLoad];

cell.backgroundColor = [UIColor whiteColor];

return cell;
}

为什么数组中的音频文件没有在集合视图单元格中以正确的顺序加载。

感谢帮助。

4

1 回答 1

0

我知道这是旧的,但无论如何都会尝试回答。

通常,当您使用集合视图时,您会指定与您希望显示的项目文件相对应的文件类型。例如,如果您正在加载图像,您可以将类型指定为 .jpg 或 .png 等。对于音频,它可能是 .aiff 或 .mp3 .wav 等。

完成此操作后,您将使用代表从 0 开始的数字的%ld。这将有助于以正确的顺序加载文件。

https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html

将为您提供有关集合视图方法替代方案的良好起点 - 这使用命名方法并适用于 .jpg 尝试将代码更改为适合音频。

于 2013-09-14T11:44:08.833 回答