集合视图中的音频序列问题didSelectItemAtIndexPath

时间:2013-06-10 20:22:51

标签: iphone ios6 indexing uicollectionview uicollectionviewcell

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

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

这是我的代码

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

为什么来自数组的音频文件未按集合视图单元格中的正确顺序加载。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我知道这已经过时了,但无论如何都会尽力回答。

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

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

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

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