我正在尝试用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:。
这是苹果公司以后要解决的问题,还是我必须做些什么?
发布于 2013-09-27 16:04:45
在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;
}
发布于 2013-10-06 08:01:58
是。正如Calman所说,您必须重写isEqual:方法来比较您所拥有的自定义属性。见苹果文档这里
如果子类和实现任何自定义布局属性,还必须重写继承的isEqual:方法,以比较属性值。在iOS 7和更高版本中,如果这些属性没有更改,集合视图就不会应用布局属性。它通过使用isEqual:方法比较新旧属性对象来确定属性是否发生了更改。因为此方法的默认实现只检查该类的现有属性,因此必须实现该方法的自己版本才能比较任何其他属性。如果您的自定义属性都相等,则调用Super并在实现结束时返回结果值。
发布于 2014-04-11 12:23:20
在这种情况下,最有效的方法是
- (BOOL)isEqual:(id)other {
if (other == self) {
return YES;
}
if(![super isEqual:other]) {
return NO;
}
return ([((MyUICollectionViewLayoutAttributes *) other) isActived] == [self isActived]);
}
https://stackoverflow.com/questions/18539874
复制相似问题