【问题标题】:Add view to subView将视图添加到子视图
【发布时间】:2015-04-28 00:13:36
【问题描述】:

我在xcode 中实现了core plot。我正在尝试将图例插入另一个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”(右边的东西)移动到另一个单元格。

【问题讨论】:

    标签: ios objective-c core-plot addsubview


    【解决方案1】:

    正如错误消息所暗示的,您正在尝试将不属于 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);
    

    【讨论】:

    • 我使用的是静态tableView。我还应该打电话给cellForRow吗?
    • 是的。 cellForRowAtIndexPath: 的实现在静态 UITableView 的实例中发生了一些变化。您从 super 检索您的单元格,而不是将可重复使用的单元格出列:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; ... configure cell ... return cell; }
    • 感谢您的回答!!我刚刚尝试过,它会将整个图表添加到该单元格中。我在问如何只添加图例,具有所有内容的名称和颜色的部分
    • 我已经更新了我的答案。对于您拥有的任何后续 cmet,我会不断更新答案以尝试帮助您,而不是多次评论。
    • 它现在将它添加到单元格中,但文本是颠倒的。 (当我删除 addSublayer 时,它是正面朝上的。)知道为什么会这样吗?
    【解决方案2】:

    使用

    CPTGraphHostingView

    放置在 UIView 中,

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多