ios:在集合视图单元格中重叠图像

时间:2015-04-24 11:18:04

标签: ios uiimageview uicollectionview

我已经以编程方式创建了集合视图,我在collectionViewCell中以编程方式创建了UIImageview,并且在imageview中显示的图像是异步从服务器下载的。

下面的代码是用cellForItemAtIndexPath:method -

编写的
#menu{
z-index:999;

问题是,当我滚动集合视图时,一些图像彼此重叠。请告诉我解决方案以避免它。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

这很有可能发生,因为您正在重复使用单元格(正确)并且之前使用的单元格仍在调用上一个图像。当单元格退出视图时,您必须取消异步任务。顺便说一下,它会让你的生活更容易使用AFNetworking + UIImageView来做到这一点。

另一个选项是在创建UIimageView之前检查单元格中是否没有UIimageView。

dispatch_sync(dispatch_get_main_queue(), ^{
    UIView *viewToRemove;
    for (UIView *view in cell.subViews) {
         if (view isKingOfClass:UIImageView){
             viewToRemove = view;
         }
    }

    [viewToRemove removeFromSuperView];

    imgvMainView =[[UIImageView alloc] initWithFrame:CGRectMake(34,41,177,124)];
    [cell addSubview:imgvMainView];

    [imgvMainView setImage:image];
    imgvMainView.contentMode = UIViewContentModeScaleAspectFit;

});

答案 1 :(得分:0)

请使用下面的行我希望这可以工作我假设重复单元格的问题。

UIImageView * imgvMainView = [[UIImageView alloc] initWithFrame:CGRectMake(34,41,177,124)]; [cell.contentView addSubview:imgvMainView];

相关问题