UICollectionView重绘到自身

时间:2014-02-15 11:52:47

标签: ios iphone objective-c uicollectionview

我是UICollectionViews的新手,我想知道我是如何做到这一点的,以便下面的代码在滚动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];

}

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

    int index = indexPath.row;

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

    UIImageView *imageViewWithEffect = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 60, 60)];

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

    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(applyFrames:) forControlEvents:UIControlEventTouchUpInside];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        UIImage *imageWithEffect = [self applyFrame:[[self.activeFrameArray 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;

}

由于

2 个答案:

答案 0 :(得分:1)

处理这种情况似乎更好的方法是拥有一个UICollectionViewCell子类ORSEffectsCell

在子类中,您将配置适当的视图层次结构,然后为这些视图提供访问器。

这意味着您要创建一次单元格视图层次结构,然后只重新配置每个单元格的内容。

答案 1 :(得分:0)

//在cellForItemAtIndexPath方法中添加此代码

for (UIView *v in [tmpView subviews])
        [v removeFromSuperview];

告诉我它的工作与否:)