我的数组中的第一个图像未显示在collectionview单元格中

时间:2016-03-21 07:58:57

标签: ios uicollectionview

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
 cell =[self.imgCllvw dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        if(!cell)
        {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        }
        NSDictionary *tmpDict = [images objectAtIndex:indexPath.row];

        NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:@"img"]];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *imge= [[UIImage alloc]initWithData:data];
              dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = imge;
              });
        });
        cell.layer.borderWidth=1.0f;
        cell.layer.borderColor=[UIColor colorWithRed:215.0/255.0 green:214.0/255.0 blue:214.0/255.0 alpha:1.0].CGColor;

                return cell;
}

未加载第一张图片,如果我滚动收藏视图,则会显示图片,并且我已将autolayout用于收藏视图

2 个答案:

答案 0 :(得分:0)

我认为你应该尝试异步下载图像而不是同步,这样的事情

utf8

答案 1 :(得分:0)

您可以使用 dispatch_sync 代替 dispatch_async,mainqueue

dispatch_sync(dispatch_get_main_queue(), ^{
    cell.imageView.image = imge;
          });