UICollectionView重复单元格?

时间:2016-01-27 05:31:33

标签: ios objective-c uicollectionview uicollectionviewcell

我已经以编程方式创建了一个UICollectionView,如下所示

 UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height-200) collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];

[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];

[vc.view addSubview:_collectionView];

当我尝试将图像添加到单元格中时,我可以看到重复的内容。以下是我的委托方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [documentImageArray count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cells = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    if(cells==nil){
        cells=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    }
    UIImageView *pic = [[UIImageView alloc] initWithFrame:cells.frame];
    pic.image=[documentImageArray objectAtIndex:indexPath.item];


    [cells addSubview:pic];
    return cells;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (IS_IPHONE_5) {
        return CGSizeMake(155, 155.f);

    } else if(IS_IPHONE_6) {
        return CGSizeMake(182.5, 182.5); //Max size

    } else if (IS_IPHONE_6P) {
        return CGSizeMake(175, 175.f);

    } else {
        return CGSizeMake(140, 140.f);  
    }        
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}

这就是结果:

enter image description here

3 个答案:

答案 0 :(得分:0)

重新使用集合视图单元格:如果将它们滚动到视线之外,Queue可以返回相同的单元格。然后,单元将处于显示时的状态 由于您向dequeueReusableCellWithReuseIdentifier:forIndexPath:添加了subView,因此subView中的collectionView:cellForItemAtIndexPath:将被移除,或者可能会与您添加的下一个subView一起显示。{br /> 你可以这样做,例如,使用

[[cells subviews] performSelector:@selector(removeFromSuperview)];

答案 1 :(得分:0)

继承UICollectionViewCell并添加UIImageView的属性。配置单元格的imageview。

@interface UIImageCollectionViewCell : UICollectionViewCell
@property (nonatomic) UIImageView *imageView;
@end

@implementation UIImageCollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
    self= [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] initWithFrame:frame];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;

        [self.contentView addSubview:self.imageView];
    }
    return self;
}

注册课程:

 [_collectionView registerClass:[UIImageCollectionViewCell class] forCellWithReuseIdentifier:@"imageCell"];

然后像这样返回单元格:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageCollectionViewCell *cell = (UIImageCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];

    cell.imageView.image = [documentImageArray objectAtIndex:indexPath.item];
    return cell;
}

答案 2 :(得分:0)

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [documentImageArray count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cells=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    if(cells==nil){
        cells=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    }
    for (UIView *view in cells.contentView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    UIImageView *pic =[[UIImageView alloc] initWithFrame:cells.frame];
    pic.image=[documentImageArray objectAtIndex:indexPath.item];


    [cells addSubview:pic];
    return cells;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(IS_IPHONE_5){
        return CGSizeMake(155, 155.f);

    }else if(IS_IPHONE_6){
        return CGSizeMake(182.5, 182.5); //Max size

    }else if (IS_IPHONE_6P){
        return CGSizeMake(175, 175.f);

    }else{
        return CGSizeMake(140, 140.f);

    }

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    }