1

在collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath方法即時嘗試加載音頻文件數組到收藏視圖單元格,但基於indexPath行它跳過索引0單元格和索引1單元格它正在播放音頻文件當細胞選擇。我無法理解爲什麼。collectionView didSelectItemAtIndexPath

這裏是我的代碼

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

} 

如果有人能在正確的方向指向我。我會感激。

感謝您的幫助。

回答

3

最後固定它

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

       }   
      } 

它可以幫助一些之一。

相關問題