在Collection View单元格中使用XYPieChart - 未显示的图表

时间:2014-04-11 18:40:32

标签: ios objective-c uitableview

我的目标是在每个名为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;
}

但是,我的问题是单元格中没有任何饼图实际显示,我也看到没有调用委托方法。我做错了什么?

谢谢!

1 个答案:

答案 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可能是零;