防止UICollectionView从重绘(Preventing UICollectionView

2019-10-19 08:27发布

我的目标是显示延迟加载缩略图的行使用(未已知数量) UICollectionView ,用户可然后水平滚动缩略图。

我使用下面的代码,以达到接近我所追求的,但我得到一个错误。

第4个缩略图加载不错,但作为您滚动缩略图开始绘制在彼此的顶部,并在随机的订单。

我想获得它,使它能够获得以下:

Item 1 ----- Item 2 ----- Item 3 ---- Item 4

我想它使细胞只画一次类似的,当你使用你会得到什么样UITableView 。 我只希望UICollectionView时,我要求它重新加载数据。

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

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

UIView *tmpView;

if([self.selectedEffects isEqualToString:@"Effects"]){

    int index = indexPath.row;

    NSLog(@"%i",index);

    tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 90)];
    [tmpView setBackgroundColor:[UIColor clearColor]];

    UIImageView *imageViewWithEffect = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 60, 60)];
    imageViewWithEffect.layer.cornerRadius = 10.00;
    imageViewWithEffect.clipsToBounds = YES;

    UILabel *labelEffect = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 70, 20)];
    [labelEffect setText:[[self.activeEffectsArray objectAtIndex:index] objectAtIndex:1]];
    labelEffect.textAlignment = NSTextAlignmentCenter;
    labelEffect.textColor = [UIColor whiteColor];
    [labelEffect setFont:[UIFont boldSystemFontOfSize:8]];

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    [activityIndicator setFrame:CGRectMake(0, 0, 60, 60)];
    activityIndicator.alpha = 1.0;
    [activityIndicator startAnimating];

    UIButton *buttonSelect = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonSelect setFrame:CGRectMake(0, 0, 60, 90)];
    [buttonSelect setTag:index];
    [buttonSelect addTarget:self action:@selector(applyEffects:) forControlEvents:UIControlEventTouchUpInside];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        UIImage *imageWithEffect = [self editImage:[[self.activeEffectsArray objectAtIndex:indexPath.row] objectAtIndex:0] ImageToEdit:self.thumbnailImage];

        dispatch_async(dispatch_get_main_queue(), ^{

            [imageViewWithEffect setImage:imageWithEffect];
            [activityIndicator removeFromSuperview];

        });

    });

    [tmpView addSubview:imageViewWithEffect];
    [tmpView addSubview:labelEffect];
    [tmpView addSubview:activityIndicator];
    [tmpView addSubview:buttonSelect];

}

[cell addSubview:tmpView];

return cell;
}

Answer 1:

您每次添加子视图的单元格。 因为细胞可能已被重用这是不正确。 检查子视图已经创建,并修改他们,如果他们已经在那里。 您可以通过继承UICollectionViewCell,或使用做到这一点viewWithTag:前者是首选)。 有数以百计的这个局面的例子,所以我不会在这里老调重弹。

您还正在引用您的调度块相同的看法。 这也是不正确的,因为电池可能已经由块被调用的时候重用。 相反,使用indexPath重新获得细胞,并对其进行修改的方式。

最后,你可能要考虑在后台线程,而不是使用四舍五入的图像边角layer.cornerRadius -这种做法将导致混合的图像,这可能会导致不连贯滚动动画时一次显示大量的图片。



文章来源: Preventing UICollectionView From Redrawing