[英]Add view to subView
I implemented core plot in xcode
. 我在
xcode
实现了核心图 。 I'm trying to insert the legend in another cell
. 我正在尝试将图例插入另一个
cell
。 Here is my code of how I implemented the legend
. 这是我如何实现
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);
Here is what I tried: 这是我尝试过的:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
[cell addSubview:theLegend];
}
I get the following error: 我收到以下错误:
Incompatible pointer types sending 'CPTGraph *' to parameter of type 'UIView *'
I understand the error, and what I did wrong. 我了解错误以及我做错了什么。 My question is, how can I add the
legend
as a subview
of a cell? 我的问题是,如何将
legend
添加为单元的subview
?
Edit 编辑
Here is what I'm talking about: 这是我在说的:
I want to move the part that has all the info - "AAPL" GOOG" "MSFT" (the thing on the right), to another cell. 我想将具有所有信息的零件-“ AAPL”,“ GOOG”,“ MSFT”(在右侧的东西)移动到另一个单元格。
As the error message suggests, you're trying to add an object that isn't of type UIView
as a subview to the cell. 如错误消息所示,您试图将不是
UIView
类型的对象添加为单元的子视图。 Add the CTPGraph object to an instance of type CPTGraphHostingView
, then add this as a subview to the cell: 所述CTPGraph对象添加到类型的实例
CPTGraphHostingView
,再加入此作为一个子视图的细胞:
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:frame];
hostingView.hostedGraph = graph;
[cell addSubview:hostingView];
Furthermore, you shouldn't really be accessing cells like that. 此外,您实际上不应该那样访问单元格。 You should be adding the graph as a subview from UITableView's delegate method,
tableView:cellForRowAtIndexPath:
. 您应该将图形添加为UITableView的委托方法
tableView:cellForRowAtIndexPath:
的子视图。 Use this method to retrieve the legend and add it to the cell before returning the completed cell. 使用此方法检索图例并将其添加到单元格中,然后返回完成的单元格。
Updated - You're using a static UITableView, so the implementation of cellForRowAtIndexPath:
differs slightly. 更新 -您正在使用静态UITableView,因此
cellForRowAtIndexPath:
的实现略有不同。 You have to retrieve the cell from super
(the Table View) instead of dequeuing a reusable cell: 您必须从
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;
}
Update 2 - To add only the legend to the cell: 更新2-仅将图例添加到单元格:
[cell.layer addSublayer:graph.legend];
Update 3 - Your text could be upside down because the coordinate system used by the legend's layer may be different. 更新3-您的文字可能会颠倒,因为图例图层使用的坐标系可能不同。 You can fix this by rotating the layer 180 degrees using a transform:
您可以通过使用变换将图层旋转180度来解决此问题:
legendLayer.transform = CATransform3DMakeRotation(M_PI / 180.0f, 0, 1, 0);
use 采用
CPTGraphHostingView
CPTGraphHostingView
to place inside UIView, 放在UIView中
Because CPTGraph
* isn't a subclass of UIView *
. 因为
CPTGraph
*不是UIView *
的子类。 You need to add the CPTGraphHostingView *hostingView
to your view by doing [cell addSubview:graph.hostingView]
; 您需要通过执行
[cell addSubview:graph.hostingView]
将CPTGraphHostingView *hostingView
添加到视图中;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.