2015-01-12 24 views
4

今天晚上在我的應用程序上工作時,我遇到了一種新情況。我試圖加載2個不同的圖形[使用BEMSimpleLineGraph]每個到UITableView的不同行。我創建了一個包含從BEMSimpleLineGraphView繼承的UIView的自定義單元格。我的視圖控制器自然地繼承自UIViewControllerBEMSimpleLineGraphDelegate,BEMSimpleLineGraphDataSource,UITableViewDataSourceUITableViewDelegate在UITableView中設置BEMSimpleLineGraph劇情

UITableViewDataSource和BEMSimpleLineGraphDataSource雙方都需要的方法,但是當我宣佈我的表視圖的numberOfRowsInSection和方法的cellForRowAtIndexPath,我不能把我的numberOfPointsInLineGraph和valueForPointAtIndex方法的cellForRowAtIndexPath裏面,因爲這不符合BEMSimpleLineGraphDataSource要求。

這是我目前等級:

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 
} 
} 

我怎麼會具有例如第一張圖從data2顯示來自data數據和第二圖形顯示的數據。我的圖表現在很好顯示,但我無法弄清楚如何爲每個圖表定義獨立的數據集。如何才能做到這一點?

+0

可以請你解釋一下你是如何整理出來的? –

回答

6

您需要檢查圖的哪個實例正在調用數據源/委託。您可以通過比較數據源/委託方法中包含的參數lineGraph與您的圖形實例進行比較。

我對swift不夠熟悉,在這裏是你如何在Objective-C中做到這一點。

- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index 
{ 
    if ([graph isEqual:self.myGraph1]) 
    { 
     return data; 
    } 
    else 
    { 
     return data2; 
    } 
} 
+1

感謝您的回覆:)不幸的是,我不久前已經想到了這個。我最終將第二個數據集保存在我的AppDelegate中,因爲它是我知道如何查找的實例。感謝羚牛回覆的時間,一切順利! – user3185748

+0

@ user3185748是不可能給一些更詳細的? –