Collectionview单元格图像没有按正确的顺序出现

时间:2015-03-16 01:04:13

标签: objective-c uicollectionview uicollectionviewcell

我所拥有的是一个指向图像的URL数组和相应的图像名称数组

例如 [' www.pictures/dog.jpg' ;,' www.pictures/lamp.jpg' ;,' www.pictures/piano.jpg'] [' dog',' lamp',' piano']

我希望每个单元格都有一个带有相应单词的图片。我的问题是图片几乎总是出现故障。我现在怎么样,单词按顺序出现。

任何人都知道如何按顺序显示我的图片

- (photoCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    photoCollectionViewCell *photoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];

    if(self.photoIndex == (self.totalPhotos)) {
        self.photoIndex = 0;
    }

    NSURL *url = [NSURL URLWithString:self.imageURLArray[self.photoIndex]];
    NSString *word = self.phraseWordsArray[self.photoIndex];

    photoCell.photoImageView.image = [UIImage imageNamed:@"icn_default"];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            photoCell.photoImageView.image = image;
            photoCell.photoLabel.text = word;
        });
    });



    self.photoIndex++;
    return photoCell;
}

1 个答案:

答案 0 :(得分:0)

当您使用dispatch_queue_t请求数据时,iOS系统将自动执行队列,因为它具有最佳性能。 因此,您的订单不符合您的预期。

参考: https://developer.apple.com/library/prerelease/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html