Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当我滚动collectionView时,它中的图像正在更改_Ios_Objective C_Uicollectionview_Uicollectionviewcell_Uicollectionviewdelegate - Fatal编程技术网

Ios 当我滚动collectionView时,它中的图像正在更改

Ios 当我滚动collectionView时,它中的图像正在更改,ios,objective-c,uicollectionview,uicollectionviewcell,uicollectionviewdelegate,Ios,Objective C,Uicollectionview,Uicollectionviewcell,Uicollectionviewdelegate,在下面的代码中,collectionView将平滑地滚动,但有时如果我滚动得非常快,则会显示一个错误的图像,然后随着滚动速度的降低而更改为正确的图像。为什么不设置 下面是我正在使用的代码 我的代码: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollect

在下面的代码中,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上有一个名为prepareforeuse的方法。如果在重新使用单元格之前未清理内容,则可能会看到上一个单元格中的内容

如果您仅使用标签/文本视图,并且您的内容已准备好进行设置,则不受影响。但是,如果您使用背景图像加载(如示例中所示)或任何其他背景任务,那么它肯定会影响您

以下是一个实施示例:

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

    [super prepareForReuse];
}

为了解决这个问题,您应该准备好要重用的单元。UICollectionViewCell和UITableViewCell上有一个名为prepareforeuse的方法。如果在重新使用单元格之前未清理内容,则可能会看到上一个单元格中的内容

如果您仅使用标签/文本视图,并且您的内容已准备好进行设置,则不受影响。但是,如果您使用背景图像加载(如示例中所示)或任何其他背景任务,那么它肯定会影响您

以下是一个实施示例:

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

    [super prepareForReuse];
}

很抱歉,我没有得到以下信息:(创建一个单元格类,它是UICollectionViewCell的子类,并实现
prepareForReuse
方法。这是一个很好的集合视图教程,但是,它是Swift。谢谢!您节省了我的时间!很抱歉,我没有得到以下信息:(创建一个单元格类,它是UICollectionViewCell的子类,并实现
prepareforeuse
方法。这是一个很好的集合视图教程,但是它是Swift。谢谢!您节省了我的时间!