ios - 当我滚动时,collectionView 中的图像正在发生变化

标签 ios objective-c uicollectionview uicollectionviewcell uicollectionviewdelegate

这个问题在这里已经有了答案:





Proper way to deal with cell reuse with background threads?

(3 个回答)


6年前关闭。




在下面的代码中,collectionView 会平滑滚动,但有时如果我滚动得非常快,则会显示不正确的图像,然后随着滚动减速而更改为正确的图像。为什么不设置
下面是我正在使用的代码
我的代码:

  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

        UICollectionViewCell *cell;
        cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];

        cell.backgroundColor = [UIColor whiteColor];

        NSMutableArray *arr_assect=[menuArray objectAtIndex:indexPath.row];

        backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];


        UIImageView *dotlines=[[UIImageView alloc]init];
        dotlines.frame=CGRectMake(1, 0, cell.bounds.size.width-1, cell.bounds.size.height);
        dotlines.image=[UIImage imageNamed:@"dottedline"];
        [cell addSubview:dotlines];

                UIImageView*cellImage=[[UIImageView alloc]initWithFrame:CGRectMake(18, 10,backView.bounds.size.height-35, backView.bounds.size.height-40)];


        NSLog(@"arrayimage%@",[[menuArray valueForKey:@"category_image"] objectAtIndex:indexPath.row]);




        NSString *str_homepage = [NSString stringWithFormat:@"%@",[[menuArray valueForKey:@"home_page"] objectAtIndex:indexPath.row]];


        NSLog(@"str_home_page%@",str_homepage);


        NSString   *imageUrl= [arr_assect valueForKey:@"category_image"];



        if ([[ImageCache sharedImageCache] DoesExist:imageUrl]) {

            cellImage.image=[UIImage imageWithData:[[ImageCache sharedImageCache] GetImage:imageUrl]];
        }
        else{


            dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

            dispatch_async(queue, ^{

                NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

                dispatch_async(dispatch_get_main_queue(), ^{
                    cellImage.image=[UIImage imageWithData:imageData];



                });
                [[ImageCache sharedImageCache] AddImage:imageUrl withData:imageData];


            });
        }

        UILabel *lblCategoryTitle =(UILabel *) [cell viewWithTag:5];

        if (!lblCategoryTitle) {

            lblCategoryTitle=[[UILabel alloc]init];

            [cell.contentView addSubview:lblCategoryTitle];
        }

        [lblCategoryTitle  setFont: [UIFont fontWithName:@"helvetica" size:10]];

        lblCategoryTitle.tag=5;

        lblCategoryTitle.textAlignment = NSTextAlignmentCenter;

        lblCategoryTitle.frame=CGRectMake(backView.frame.origin.x, cellImage.frame.origin.y+cellImage.frame.size.height+0,cell.frame.size.width , 25);

        lblCategoryTitle.textColor = [UIColor blackColor];

        lblCategoryTitle.backgroundColor = [UIColor clearColor];

        lblCategoryTitle.numberOfLines = 2;




        NSString *abc = [arr_assect valueForKey:@"categoryNm"];


        abc = [NSString stringWithFormat:@"%@%@",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1] ];
        NSLog(@"abc = %@",abc);


        if (APP_SHARE.language_int == 0) {
            lblCategoryTitle.text =abc;
        }
        else if (APP_SHARE.language_int == 2)
        {
            lblCategoryTitle.text =[arr_assect valueForKey:@"categoryNmBl"];
        }

        [cell addSubview:cellImage];
        [cell.contentView addSubview:backView];
        return cell;

    }

最佳答案

为了解决这个问题,您应该准备好您的单元格以供重复使用。有一种方法叫做 准备重用 在 UICollectionViewCell 和 UITableViewCell 上。如果您在重用单元格之前不清理内容,那么您可能会看到前一个单元格中的内容。

如果您只使用标签/ TextView 并且您的内容已准备好设置,它不会影响。但是,如果您使用背景图像加载(如您的示例中)或任何其他后台任务,那么它肯定会影响您。

下面是一个实现示例:

- (void)prepareForReuse {
    _titleLabel.text = nil;
    _avatarImageView.image = nil;

    [super prepareForReuse];
}

关于ios - 当我滚动时,collectionView 中的图像正在发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196440/

相关文章:

objective-c - 阻止父 View 在 subview 对其进行操作后接收触摸事件

objective-c - 如何访问 UISwipeGestureRecognizer 的 'direction' 属性?

swift - 如何使 Collection View 中的一个单元格的高度与屏幕减去导航高度相同。酒吧?

ios - 如何设置动态宽度 UICollectionViewCell

iOS 从 Xcode 项目更新或卸载 pod

ios - SCNVector3 组件的类型是什么? CGFloat 还是 float ?

objective-c - 取消隐藏光标滞后

ios - 如何在 UIView 的顶部添加边框

Swift 3 泛型 : issue with setting UICollectionViewDatasource & Delegate for a UICollectionView inside UITableViewCell

ios - RxSwift - 在 X 秒内未收到元素后发出