【问题标题】:How to refresh UICollectionViewCell in iOS 7?如何在 iOS 7 中刷新 UICollectionViewCell?
【发布时间】:2013-08-30 19:04:48
【问题描述】:

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

我有一个自定义的 UICollectionViewLayoutAttributes。

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

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

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

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

但是,在 iOS 7 中,即使我重新加载 CollectionView,- applyLayoutAttributes: 也不会被调用。

这是苹果稍后会修复的错误,还是我必须做点什么?

【问题讨论】:

    标签: uicollectionview uicollectionviewcell ios7 xcode5


    【解决方案1】:

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

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

    试试这个:

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

    【讨论】:

    • 原来我需要用return [super isEqual:other]替换最后一个return YES
    【解决方案2】:

    是的。正如 Calman 所说,您必须重写 isEqual: 方法来比较您拥有的自定义属性。参见苹果文档here

    如果您继承并实现任何自定义布局属性,您还必须重写继承的 isEqual: 方法来比较您的属性值。在 iOS 7 及更高版本中,如果这些属性未更改,则集合视图不会应用布局属性。它通过使用 isEqual: 方法比较新旧属性对象来确定属性是否已更改。由于此方法的默认实现仅检查此类的现有属性,因此您必须实现您自己的方法版本才能比较任何其他属性。如果您的自定义属性都相等,请调用 super 并在实现结束时返回结果值。

    【讨论】:

      【解决方案3】:

      在这种情况下,最有效的方法是

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

      【讨论】:

        猜你喜欢
        • 2013-11-23
        • 2014-03-30
        • 2014-01-22
        • 2015-07-29
        • 1970-01-01
        • 1970-01-01
        • 2014-11-14
        • 2014-11-03
        • 2013-12-26
        相关资源
        最近更新 更多