如果滚动,UICollectionView应用程序崩溃

时间:2013-07-09 10:54:59

标签: ios objective-c uicollectionview uicollectionviewcell

我正面临一个问题。我创建了一个UICollectionView,如果我滚动那么多,我再也看不到第一行了,那么App崩溃了。我真的不知道如何解决这个问题。这是我创建CollectionView的应用程序的一部分。单元格的内容是图像,其中包含与iTunes同步的所有电影视频的缩略图。一切正常,但不是滚动超过1行。

- (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 个答案:

答案 0 :(得分:3)

似乎超出范围异常的索引。把这个:

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

而不是

MPMediaItem *item = [itemList objectAtIndex:cellMediaItemCounter];