【问题标题】:Subclass does not inherit its functionality子类不继承其功能
【发布时间】: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


【解决方案1】:

您从错误的类继承。 itemSize 属性是 UICollectionViewFlowLayout 的一部分,而不是 UICollectionViewLayout

另外,您可能想致电[super layoutAttributesForElementsInRect:rect]。将该消息发送到self 将由于无限递归而导致堆栈溢出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 2013-11-29
    • 1970-01-01
    • 2012-03-09
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多