collectionView didSelectItemAtIndexPath

В методе collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath я пытаюсь загрузить массив аудиофайлов в ячейки представления коллекции, но на основе строки indexPath он пропускает ячейку с индексом 0, а из ячейки с индексом 1 воспроизводит аудиофайл при выборе ячейки. я не могу понять, почему.

Вот мой код

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

}

Если кто-то может указать мне в правильном направлении. Я буду благодарен.

Спасибо за помощь.


person user1120133    schedule 11.06.2013    source источник


Ответы (1)


Наконец-то исправил

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

               }        
            }

Это может помочь кому-то.

person user1120133    schedule 11.06.2013