[英]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.