How to refresh UICollectionViewCell in iOS 7?

蹲街弑〆低调 提交于 2019-12-03 00:02:32

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;
}

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.

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]);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!