繁体   English   中英

将视图添加到subView

[英]Add view to subView

我在xcode实现了核心图 我正在尝试将图例插入另一个cell 这是我如何实现legend代码。

- (void)configureLegend
{
    CPTGraph *graph = self.hostView.hostedGraph;
    CPTLegend *theLegend = [CPTLegend legendWithGraph:graph];

    // Configure the legend
    theLegend.numberOfColumns = 1;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 5.0;

    // Add legend to graph
    graph.legend = theLegend;
    graph.legendAnchor = CPTRectAnchorRight;
    CGFloat legendPadding = - (self.chartView.bounds.size.width / 8);
    graph.legendDisplacement = CGPointMake(legendPadding, 0.0);

这是我尝试过的:

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    [cell addSubview:theLegend];
}

我收到以下错误:

Incompatible pointer types sending 'CPTGraph *' to parameter of type 'UIView *'

我了解错误以及我做错了什么。 我的问题是,如何将legend添加为单元的subview

编辑

这是我在说的:

在此处输入图片说明

我想将具有所有信息的零件-“ AAPL”,“ GOOG”,“ MSFT”(在右侧的东西)移动到另一个单元格。

如错误消息所示,您试图将不是UIView类型的对象添加为单元的子视图。 所述CTPGraph对象添加到类型的实例CPTGraphHostingView ,再加入此作为一个子视图的细胞:

CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:frame];
hostingView.hostedGraph = graph;

[cell addSubview:hostingView];

此外,您实际上不应该那样访问单元格。 您应该将图形添加为UITableView的委托方法tableView:cellForRowAtIndexPath:的子视图。 使用此方法检索图例并将其添加到单元格中,然后返回完成的单元格。

更新 -您正在使用静态UITableView,因此cellForRowAtIndexPath:的实现略有不同。 您必须从super (表视图)中检索单元格,而不是使可重用单元格出队:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    ...retrieve instance of graph...

    CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:frame];
    hostingView.hostedGraph = graph;

    [cell addSubview:hostingView];

    return cell;
}

更新2-仅将图例添加到单元格:

[cell.layer addSublayer:graph.legend];

更新3-您的文字可能会颠倒,因为图例图层使用的坐标系可能不同。 您可以通过使用变换将图层旋转180度来解决此问题:

legendLayer.transform = CATransform3DMakeRotation(M_PI / 180.0f, 0, 1, 0);

采用

CPTGraphHostingView

放在UIView中

因为CPTGraph *不是UIView *的子类。 您需要通过执行[cell addSubview:graph.hostingView]CPTGraphHostingView *hostingView添加到视图中;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM