Reputation: 693
I implemented core plot in xcode
. I'm trying to insert the legend in another cell
. Here is my code of how I implemented the 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?
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.
Upvotes: 0
Views: 121
Reputation: 2192
As the error message suggests, you're trying to add an object that isn't of type UIView
as a subview to the cell. Add the CTPGraph object to an instance of type CPTGraphHostingView
, then add this as a subview to the cell:
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:
. 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. You have to retrieve the cell from super
(the Table View) instead of dequeuing a reusable cell:
- (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:
[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. You can fix this by rotating the layer 180 degrees using a transform:
legendLayer.transform = CATransform3DMakeRotation(M_PI / 180.0f, 0, 1, 0);
Upvotes: 1
Reputation: 5536
Because CPTGraph
* isn't a subclass of UIView *
. You need to add the CPTGraphHostingView *hostingView
to your view by doing [cell addSubview:graph.hostingView]
;
Upvotes: 0