0

我正在尝试在我的自定义上设置一个属性UICollectionViewCell

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Setup cell identifier
    static NSString *cellIdentifier = @"IgCell";

    PAInterestGraphCell *cell = (PAInterestGraphCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    cell.interestCategory = [self.dataArray objectAtIndex:indexPath.row];

    // Return the cell
    return cell;

}

PAInterestGraphCell代码:

@interface PAInterestGraphCell : UICollectionViewCell

@property (atomic, retain) PAInterestCategory *interestCategory;

@end


#import "PAInterestGraphCell.h"
#import "PAScaleView.h"

@implementation PAInterestGraphCell

@synthesize interestCategory;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        PAScaleView *scaleView = [[PAScaleView alloc] initWithFrame:frame];
        scaleView.backgroundColor = [UIColor clearColor];
        scaleView.interestCategory = self.interestCategory;

        [self.contentView addSubview:scaleView];
    }

    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

但是当我访问它的interestCategory属性时,initWithFrame它已经失去了所有的价值。

我在这里遗漏了一些明显的东西吗?

4

1 回答 1

0

在“”方法结束之前, “ @synthesize”访问器可能不会完全创建init,因此如果您需要在“ init”方法期间访问它们,则需要使用静态变量或 ivar(然后让您的“ @synthesize”指向它们并在“ self.”之外的所有内容中使用“ init”)。

另外,您想要访问interestCategory在对象实例化和初始化之前无法设置的“”属性似乎有点毫无意义。或者那是你在剪切和粘贴中遗漏的一行代码?

于 2012-12-08T19:38:16.750 回答