Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义UICollectionViewCell可以';t设置属性值_Ios - Fatal编程技术网

Ios 自定义UICollectionViewCell可以';t设置属性值

Ios 自定义UICollectionViewCell可以';t设置属性值,ios,Ios,我正在尝试在自定义的UICollectionViewCell上设置属性: -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // Setup cell identifier static NSString *cellIdentifier = @"IgCell"; PAIn

我正在尝试在自定义的
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
但是当我访问
initWithFrame
中的
interestCategory
属性时,它丢失了所有的值

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

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


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

关于访问interestCategory,我们在谈论哪一行?在您的“
init
”方法中的“
scaleView.interestCategory=self.interestCategory;
”行。我看不出它是在您的
init
方法中初始化的。在您对它调用“
alloc
”&“
init
”之前,它不可能被初始化,是吗?它应该在
cellForItemAtIndexPath
方法中设置:
cell.interestCategory=[self.dataArray ObjectAtIndexath.row]
initWithFrame
”应该在第一次调用“
cellForItemAtIndexPath
”时被调用,直到出现足够的单元格填满屏幕,然后“
dequeueReusableCellWithReuseIdentifier
”不必实例化其他单元格,而是可以回收以前使用过的(和屏幕外)单元格。您需要通过静态或“
PAInterestGraphCell
”对象ivar设置“
interestCategory
”属性,或通过“
PAInterestGraphCell
”对象init以外的其他地方的属性访问器设置它。何时何地为interestCategory设置任何值?我看到您是在表视图中这样做的,但是为什么您希望它在init方法中有任何值呢?我将
PAInterestCategory
的8个实例添加到
dataArray
,然后在
cellForItemAtIndexPath
中,我在索引处得到一个实例,并将其分配给单元格。是的,我看到了,但在您的问题中,你说它在init方法中是nil——为什么你希望它在那里有任何值?你还没有设置它。我应该在我的单元格中重写什么方法,在那里我可以添加子视图并获取值?我不太明白你想做什么——你不是从单元格中“获取”值,而是从数组中为它们设置值。你在当前代码中看到了什么?牢房里有人吗?