Mrugesh Tank
Mrugesh Tank

Reputation: 3560

UICollectionView reloading not properly

I have that kind of requirement, if user has selected 1 then I have to display images (which is in collection view cell) by rotating at 90 degree and if user had selected 4 then I have to display images as round. For selection 2 and 3 display image as it is.
For that I written below code.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilterCell *cell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"filterCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[FilterCell alloc] init];
    }
        if (filterScreen==1) {
            cell.imgView.image = [collectionImage objectAtIndex:indexPath.row];
            cell.imgView.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
        else if (filterScreen==2) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"filter_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==3) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"motiontitle_contents_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==4) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bgm_%ld",(long)indexPath.row]];
            cell.imgView.layer.cornerRadius = cell.imgView.frame.size.height /2;
            cell.imgView.layer.masksToBounds = YES;
        }
    return cell;
}

and on selection 1,2,3 or 4 I'm reloading collection view.

The problem is that if user selected 4 after selecting 1 then some of images after displays rotated and after that if user selects 2 or 3 some images rotated and some images circle. I have tried to delete section before reloading as per mention here but app crashes and log is

*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.93/UICollectionView.m:3917

reloading code is as below:

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,0)];
[filterCollectionView deleteSections:indexSet];
[filterCollectionView reloadData];

UPDATE: If tried to using
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,1)];
then getting

*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.93/UICollectionView.m:3901

can anyone help me to solve this?

Upvotes: 0

Views: 672

Answers (1)

cutsoy
cutsoy

Reputation: 10251

Okay it took a while for me to understand your problem.

First, the solution you are trying is wrong. You want to reload a section but delete it instead (try reloadSections instead).

However, that won't solve your problem either. Cells are reused and you never reset the transform property. Try this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FilterCell *cell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"filterCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[FilterCell alloc] init];
    }
    cell.imgView.transform = CGAffineTransformIdentity;
    cell.imgView.layer.cornerRadius = 0;
    cell.imgView.layer.masksToBounds = NO;

        if (filterScreen==1) {
            cell.imgView.image = [collectionImage objectAtIndex:indexPath.row];
            cell.imgView.transform = CGAffineTransformMakeRotation(M_PI_2);
        }
        else if (filterScreen==2) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"filter_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==3) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"motiontitle_contents_%ld",(long)indexPath.row]];
        }
        else if (filterScreen==4) {
            cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bgm_%ld",(long)indexPath.row]];
            cell.imgView.layer.cornerRadius = cell.imgView.frame.size.height /2;
            cell.imgView.layer.masksToBounds = YES;
        }
    return cell;
}

Upvotes: 2

Related Questions