首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >collectionView didSelectItemAtIndexPath

collectionView didSelectItemAtIndexPath
EN

Stack Overflow用户
提问于 2013-06-12 02:54:33
回答 1查看 4K关注 0票数 1

在collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath方法中,我试图将音频文件数组加载到集合视图单元格中,但基于indexPath行,它跳过了索引0处的单元格和索引1处的单元格,当单元格被选中时,它正在播放音频文件。我不明白为什么。

以下是我的代码

代码语言:javascript
复制
- (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:filePath ofType: @"mp3"];

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

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

}

如果有人能给我指出正确的方向。我会很感激的。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-12 05:31:22

终于修好了

代码语言:javascript
复制
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([[segue identifier] isEqualToString:@"showDetail"])
{
    NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

    // load the image, prevent it from being cached using 'initWithContentsOfFile'

    NSString *imageNameToLoad = [NSString stringWithFormat:@"%d_full", selectedIndexPath.item];

    NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];


    NSString *audioToLoad = [NSString stringWithFormat:@"%d", selectedIndexPath.item];


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

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

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

    DetailViewController *detailViewController = [segue destinationViewController];

    detailViewController.image = image;

    detailViewController.audioPlayer = audioPlayer;

               }        
            }

这可能会对某些人有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17051647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档