自定义UICollectionViewCell无法设置属性值

时间:2012-12-08 19:35:01

标签: ios

我正在尝试在自定义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属性时,它已丢失所有值。

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

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

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