今天晚上在我的应用程序上工作时,我遇到了一个新的情况。我试图使用BEMSimpleLineGraph加载两个不同的图,每个图都加载到UITableView的不同行中。我已经创建了一个包含从UIView继承的BEMSimpleLineGraphView的自定义单元格。我的视图控制器自然继承自UIViewController、BEMSimpleLineGraphDelegate、BEMSimpleLineGraphDataSource、UITableViewDataSource和UITableViewDelegate。
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
}
}例如,我将如何让第一个图形显示来自data的数据和第二个图形显示来自data2的数据。我的图表现在显示得很好,但我不知道如何为每个图定义独立的数据集。这是如何做到的呢?
发布于 2015-02-27 22:42:44
您需要检查图形的哪个实例正在调用数据源/委托。您可以通过将数据源/委托方法中包含的参数lineGraph与图的实例进行比较来找到答案。
我对斯威夫特还不够熟悉,在这里你可以在目标C中做到这一点。
- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index
{
if ([graph isEqual:self.myGraph1])
{
return data;
}
else
{
return data2;
}
}https://stackoverflow.com/questions/27894214
复制相似问题