Dragon warrior
Dragon warrior

Reputation: 1644

How to refresh UICollectionViewCell in iOS 7?

I am trying to develop my app in Xcode 5 and debug it under iOS 7 environment.

I have a customized UICollectionViewLayoutAttributes.

I plan to do something after long pressing on UICollectionViewCell, so I override the method in UICollectionViewCell.m

- (void)applyLayoutAttributes:(MyUICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    if ([(MyUICollectionViewLayoutAttributes *)layoutAttributes isActived])
    {
        [self startShaking];
    }
    else
    {
        [self stopShaking];
    }
}

In iOS 6 or below, - applyLayoutAttributes: is called after I call the statements below.

UICollectionViewLayout *layout = (UICollectionViewLayout *)self.collectionView.collectionViewLayout;
[layout invalidateLayout];

However, in iOS 7, - applyLayoutAttributes: is NOT being called even if I reload the CollectionView.

Is that a bug which is gonna be fixed by Apple later on, or I have to do something?

Upvotes: 9

Views: 3929

Answers (3)

Phil
Phil

Reputation: 2794

In this case, the most efficient method would be

- (BOOL)isEqual:(id)other {
        if (other == self) {
            return YES;
        }

        if(![super isEqual:other]) {
            return NO;
        }

        return ([((MyUICollectionViewLayoutAttributes *) other) isActived] == [self isActived]);
}

Upvotes: 1

Jirune
Jirune

Reputation: 2340

Yes. As Calman said you must override isEqual: method to compare custom properties that you have. See the apple documentation here

If you subclass and implement any custom layout attributes, you must also override the inherited isEqual: method to compare the values of your properties. In iOS 7 and later, the collection view does not apply layout attributes if those attributes have not changed. It determines whether the attributes have changed by comparing the old and new attribute objects using the isEqual: method. Because the default implementation of this method checks only the existing properties of this class, you must implement your own version of the method to compare any additional properties. If your custom properties are all equal, call super and return the resulting value at the end of your implementation.

Upvotes: 2

Calman
Calman

Reputation: 546

In iOS 7, you must override isEqual: in your UICollectionViewLayoutAttributes subclass to compare any custom properties that you have.

The default implementation of isEqual: does not compare your custom properties and thus always returns YES, which means that -applyLayoutAttributes: is never called.

Try this:

- (BOOL)isEqual:(id)other {
    if (other == self) {
            return YES;
    }
    if (!other || ![[other class] isEqual:[self class]]) {
            return NO;
    }
    if ([((MyUICollectionViewLayoutAttributes *) other) isActived] != [self isActived]) {
        return NO;
    }

    return YES;
}

Upvotes: 23

Related Questions