0

I have double items in my collectionView. How can I fix it?

I have UITableViewCell with UICollectionViewCell inside it.

I set elements size and direction based on its count.

enter image description here

My cell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.layout.itemSize = CGSizeMake(70, 70);
        self.layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.collectionView = [[IndexedCollectionView alloc] initWithFrame:CGRectMake(10, 250, 60, 320) collectionViewLayout:self.layout];
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.contentView addSubview:self.collectionView];

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)layoutSubviews
{
    [super layoutSubviews];

    self.collectionView.frame = self.contentView.bounds;
}

-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index
{
    self.collectionView.dataSource = dataSourceDelegate;
    self.collectionView.delegate = dataSourceDelegate;
    self.collectionView.index = index;

    [self.collectionView reloadData];
}

Delegate Methods

-(FeedCell *)collectionView:(IndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FeedCell *cell = (FeedCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];

    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = collectionView.frame;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;

    NSInteger count = [self test:collectionView].count;
    if (count == 0)
    {
        return nil;
    }

    NSInteger imageIndex = 0;

    if (count == 1)
    {
        [imageView setFrame:CGRectMake(0, 110, 310, 200)];
        imageIndex = indexPath.item;
        imageView.contentMode = UIViewContentModeScaleAspectFit;

    }
    if (count == 2)
    {
        [imageView setFrame:CGRectMake(0, 110, 150, 200)];
        imageIndex = indexPath.item;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    if (count == 3)
    {
        [imageView setFrame:CGRectMake(10, 100, 90, 90)];
        imageIndex = indexPath.item;
    }
    if (count == 4)
    {
        [imageView setFrame:CGRectMake(10, 100, 130, 130)];//2lines
        imageIndex = indexPath.item;
    }
    if (count == 5 || count == 6)
    {
        [imageView setFrame:CGRectMake(10, 100, 90, 90)];//2lines
        imageIndex = indexPath.item;
    }
    if (count > 6)
    {
        [imageView setFrame:CGRectMake(10, 100, 100, 100)];//2lines
        imageIndex = indexPath.item ;
        //cell.layout.itemSize = CGSizeMake(50, 50);
    }


    imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];     

    [cell.contentView addSubview:imageView];


    return cell;
}

2 Answers2

0

The way you create the collectionView Cell in the table view Cell is not correct. Here are some references you can refer to create a collection view.

Collection View Cell Apple Reference.

Create Collection View without Storyboard

DilumN
  • 2,889
  • 6
  • 30
  • 44
0

Refer to my answer here. You need to subclass your cells.

What is happening is you are dequeuing an existing cell, and then adding in another subview over it. Most of the users on here argue that the ONLY way to fix this is to go through the main subclassing method. It is faster and more "formal", however, you can add another tool to your toolbox:

Instead, you can test if the subview has already been added WITHOUT subclassing, and just stick it in a logic block. Here is what you can do:

BOOL doesContain = [cell.subviews containsObject:imageView]; 
if (!doesContain)
{
    [cell.contentView addSubview:imageView];
}
imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];
Community
  • 1
  • 1
Michael Lorenzo
  • 628
  • 10
  • 20