如何在iOS 7中刷新UICollectionViewCell?

问题描述:

我正在尝试在Xcode 5中开发我的应用程序并在iOS 7环境下进行调试。

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

我有一个自定义的UICollectionViewLayoutAttributes。

I have a customized UICollectionViewLayoutAttributes.

我计划在长按UICollectionViewCell后做一些事情,所以我重写了UICollectionViewCell.m中的方法

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

在iOS 6或更低版本中, - applyLayoutAttributes在我调用下面的语句后调用

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

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

然而,在iOS 7中, - applyLayoutAttributes:甚至没有被调用如果我重新加载CollectionView。

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?

在iOS 7中,您必须在UICollectionViewLayoutAttributes子类中覆盖isEqual:以比较您拥有的任何自定义属性。

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

isEqual的默认实现:不比较自定义属性,因此总是返回YES,这意味着-applyLayoutAttributes:永远不会被调用。

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

试试这个:

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