4

While working on my app this evening I've come across a new situation. I'm attempting to load 2 different graphs [using BEMSimpleLineGraph] each into a different row of a UITableView. I've created a custom cell containing a UIView that inherits from BEMSimpleLineGraphView. My view controller naturally inherits from UIViewController, BEMSimpleLineGraphDelegate, BEMSimpleLineGraphDataSource, UITableViewDataSource, and UITableViewDelegate.

UITableViewDataSource and BEMSimpleLineGraphDataSource both have required methods but when I declare my table view's numberOfRowsInSection and cellForRowAtIndexPath methods, I can't place my numberOfPointsInLineGraph and valueForPointAtIndex methods inside of cellForRowAtIndexPath as this doesn't meet the requirements for BEMSimpleLineGraphDataSource.

Here is my current class:

import Foundation

class FlightsDetailViewController: UIViewController, BEMSimpleLineGraphDelegate, BEMSimpleLineGraphDataSource, UITableViewDataSource, UITableViewDelegate {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell

    cell.userInteractionEnabled = false

    cell.graphView.enableBezierCurve = true
    cell.graphView.enableReferenceYAxisLines = true
    cell.graphView.enableYAxisLabel = true
    cell.graphView.colorYaxisLabel = UIColor.whiteColor()

    cell.graphView.delegate = self
    cell.graphView.dataSource = self

    return cell

}

func lineGraph(graph: BEMSimpleLineGraphView!, valueForPointAtIndex index: Int) -> CGFloat {
    let data = [1.0,2.0,3.0,2.0,0.0]
    let data2 = [2.0,0.0,2.0,3.0,5.0]
    return CGFloat(data[index])
}

func numberOfPointsInLineGraph(graph: BEMSimpleLineGraphView!) -> Int {
    return 5
}
}

How would I for example have the first graph display the data from data and the second graph display data from data2. My graphs are displaying fine now but I can't figure out how to define independent data sets for each graph. How can this be done?

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
user3185748
  • 2,478
  • 8
  • 27
  • 43

1 Answers1

6

You need to check which instance of your graph is calling the datasource/delegate. You can find out by comparing the parameter lineGraph included in the datasource/delegate methods, to your instances of the graph.

I'm not familiar enough with swift, be here is how you can do it in Objective-C.

- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index 
{
    if ([graph isEqual:self.myGraph1])
    {
        return data;
    }
    else
    {
        return data2;
    }
}
BorisE
  • 385
  • 2
  • 7
  • 1
    Thanks for the reply :) unfortunately I had already figured this one out a while ago. I ended up just keeping the second dataset in my AppDelegate since it was an instance that I knew how to find. Thanks for takin the time to reply though, all the best! – user3185748 Feb 28 '15 at 18:47
  • @user3185748 isn't possible to give some more detailed? – linto jacob Nov 08 '17 at 11:02