2013-07-09 36 views
0

我面臨一個問題。我創建了一個UICollectionView,如果我滾動那麼多,我不能再看到第一行,那麼應用程序崩潰。我真的不知道如何解決這個問題。這是我的應用程序的一部分,我創建了CollectionView。單元格的內容是圖像,它們保存着與我的iTunes同步的所有視頻的縮略圖。一切正常,但不是滾動多於一行。UICollectionView應用程序崩潰,如果滾動

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; 
{ 
    return itemList.count; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    // we're going to use a custom UICollectionViewCell, which will hold an image and its label 

    //NSLog(@"%@",indexPath); 
    VPCell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath]; 


    // make the cell's title from title of current MPMediaItem 

    MPMediaItem *item = [itemList objectAtIndex:cellMediaItemCounter]; 
    NSString* title = [item valueForProperty:MPMediaItemPropertyTitle]; 
    cell.label.text = [NSString stringWithFormat:@"%@", title]; 

    NSNumber *duration=[item valueForProperty:MPMediaItemPropertyPlaybackDuration]; 
    double seconds = duration.doubleValue; 

    int secondsInt = round(seconds); 
    int minutes = secondsInt/60; 
    secondsInt -= minutes*60; 

    cell.durationLabel.text = [NSString stringWithFormat:@"%.2i:%.2i", minutes, secondsInt]; 

    // load the thumbnail for this cell 

    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL]; 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
    UIImage *thumbnail = [player thumbnailImageAtTime:13.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; 

    //set thumbnail to cell image 
    cell.image.image = thumbnail; 

    cellMediaItemCounter++; 
    //pause initiated player (to get thumbnail), if not it's playing in background 
    player=nil; 
    return cell; 
} 

非常感謝。

+1

是什麼錯誤說? – Zen

+0

什麼版本的ios? –

+0

來自@Vame的解決方案非常完美! – kchromik

回答

3

看起來像索引超出範圍例外。把這個:的

MPMediaItem *item = [itemList objectAtIndex:indexPath.row]; 

代替

MPMediaItem *item = [itemList objectAtIndex:cellMediaItemCounter]; 
+0

或者'indexPath.item'。 – Rob

+0

謝謝!我現在工作正常。問題實際上是一個「索引超出範圍例外」。 – kchromik

相關問題