我的目标是在每个名为UICollectionViewCells
的子PictureCollectionCell
中添加XYPieChart。实际饼图视图(称为pieChart
)连接到Storyboard中的原型单元格。
目前,我将PictureCollectionCell
设置为数据源和委托的委托,并在单元格initWithCoder
中执行以下操作:
- (id)initWithCoder:(NSCoder *)coder
{
NSLog(@"Called?");
self = [super initWithCoder:coder];
if (self){
// Initialization code
[self.pieChart setDataSource:self];
[self.pieChart setDelegate:self];
//Note: the actual pie slices are set inside the cells
[self.pieChart reloadData];
}
return self;
}
(我还在PictureCollectionCell
中实现了XYChart 委托和数据源方法,我省略了这里的方法。)
在CollectionViewController
内,我有以下内容:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSNumber *sliceOne = [NSNumber numberWithInt:33];
NSNumber *sliceTwo = [NSNumber numberWithInt:33];
cell.slices = @[sliceOne, sliceTwo];
[cell.pieChart reloadData];
return cell;
}
但是,我的问题是单元格中没有任何饼图实际显示,我也看到没有调用委托方法。我做错了什么?
谢谢!
答案 0 :(得分:3)
将以下内容移至方法awakeFromNib:
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self.pieChart setDataSource:self];
[self.pieChart setDelegate:self];
//Note: the actual pie slices are set inside the cells
[self.pieChart reloadData];
}
原因是在initWithCoder中,插座还没有接线,所以那时pieChart可能是零;