Question

enter image description here

I've got this problem with a UICollectionView inside a UICollectionViewCell: The last UICollectionViewCell doesn't get filled with 13 - 18, but instead, the first one get's copied. I know it has something to do with "dequeueReusableCellWithReuseIdentifier" but if I don't do the check on the tag below, my entire collection gets messed up.

I've been struggling with this issue and Googling around for about a day now, but I can't find how to fix this.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID=@"pageCell";
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    int no = indexPath.item;
    if(collectionView == _pageCollectionView)
    {
        //Collection pages
        cell.tag=no+1;
        UICollectionViewFlowLayout *layoutMods=[[UICollectionViewFlowLayout alloc] init];
        [layoutMods setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [layoutMods setMinimumInteritemSpacing:0];
        [layoutMods setMinimumLineSpacing:0];
        [layoutMods setItemSize:CGSizeMake(125, 125)];
        _modsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0,0, _pageCollectionView.frame.size.width,  _pageCollectionView.frame.size.height) collectionViewLayout:layoutMods];
        [_modsCollectionView setDataSource:self];
        [_modsCollectionView setDelegate:self];
        _modsCollectionView.pagingEnabled = YES;
        [_modsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pageCell"];

        NSMutableArray *randColor = [[NSMutableArray alloc] init];
        for(int i=0;i<3;i++)
        {
            int lowerBound = 0;
            int upperBound = 255;
            NSNumber *rndValue = [NSNumber numberWithFloat:(lowerBound + arc4random() % (upperBound - lowerBound) )/255.0];
            [randColor insertObject:rndValue atIndex:i];
        }

        float r = [[randColor objectAtIndex:0] floatValue];
        float g = [[randColor objectAtIndex:1] floatValue];
        float b = [[randColor objectAtIndex:2] floatValue];
        _modsCollectionView.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1];
        [cell addSubview:_modsCollectionView];
    }
    else
    {
        if(cell.tag == 0)
        {
            cell.tag = 2;

            NSMutableDictionary *mod=[modules objectAtIndex:modulePointer];

            cell.backgroundColor=[mod objectForKey:@"color"];

            NSString *slug = [mod objectForKey:@"slug"];

            NSString *imgName=[NSString stringWithFormat:slug,@".png"];
            UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]];
            customBackground.frame = CGRectMake(0,0, 125, 125);
            [cell addSubview:customBackground];
            customBackground.contentMode = UIViewContentModeScaleAspectFit;
            customBackground.clipsToBounds = YES;
            modulePointer++;
        }
    }
    return cell;
}

What am I doing wrong?

Was it helpful?

Solution

Got it! I had to work some magic using view tags inside - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath.

In the if(collectionView == _pageCollectionView) i put:

for(id subview in [cell subviews])
{
      [subview removeFromSuperview];
}
cell.tag=itemsPerScreen*no;

And then in the following else:

int thisIndex=[collectionView superview].tag+no;
NSMutableDictionary *mod=[modules objectAtIndex:thisIndex];

OTHER TIPS

You need to create your own custom UICollectionViewCell to prevent this from happening.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top