【发布时间】:2015-08-24 01:36:09
【问题描述】:
我正在尝试创建 UICollectionViewLayout 的子类,但我不能调用它的原始功能,例如 itemSize 并且我收到错误提示
在对象“子类 *”上找不到属性“itemSize”
我错过了什么?
我的代码如下所示。
.h
#import <UIKit/UIKit.h>
@interface SubClass : UICollectionViewLayout
@end
.m
#import "SubClass.h"
@implementation SubClass
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *answer = [self layoutAttributesForElementsInRect:rect];
for(int i = 1; i < [answer count]; ++i) {
UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
CGFloat maximumSpacing = 20.0f;
CGFloat origin = CGRectGetMaxX(prevLayoutAttributes.frame);
if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
}
}
return answer;
}
@end
【问题讨论】:
-
itemSize属性是UICollectionViewFlowLayout的一部分,而不是UICollectionViewLayout。但是您发布的代码没有提到itemSize。向我们展示包含错误的代码。 -
此外,您发布的代码会递归调用
layoutAttributesForElementsInRect:,因此即使它已编译,您也会在运行时遇到堆栈溢出。你可能是想说[super layoutAttributesForElementsInRect:rect]。 -
噢!我真傻,昨天太累了。我将其更改为
UICollectionViewFlowLayout现在它工作正常。我也把它改成了超级,而不是自我。
标签: objective-c inheritance compiler-errors subclass