在UITableView中设置BEMSimpleLineGraph绘图
今天晚上在我的应用程序上工作时,我遇到了一个新情况。我正在尝试将两个不同的图形[使用BEMSimpleLineGraph]加载到UITableView的不同行中。我创建了一个自定义单元格,其中包含从BEMSimpleLineGraphView继承的UIView。我的视图控制器自然继承自在UITableView中设置BEMSimpleLineGraph绘图,uitableview,swift,graph,bemsimplelinegraph,Uitableview,Swift,Graph,Bemsimplelinegraph,今天晚上在我的应用程序上工作时,我遇到了一个新情况。我正在尝试将两个不同的图形[使用BEMSimpleLineGraph]加载到UITableView的不同行中。我创建了一个自定义单元格,其中包含从BEMSimpleLineGraphView继承的UIView。我的视图控制器自然继承自UIViewController,BEMSimpleLineGraphDelegate,BEMSimpleLineGraphDataSource,UITableViewDataSource和UITableViewD
UIViewController
,BEMSimpleLineGraphDelegate
,BEMSimpleLineGraphDataSource
,UITableViewDataSource
和UITableViewDelegate
UITableViewDataSource和BEMSimpleLineGraphDataSource都有必需的方法,但当我声明表视图的numberOfRowsInSection和cellForRowAtIndexPath方法时,我无法将我的numberOfPointsInLineGraph和ValueForPointIndex方法放在CellForRowatinIndexPath中,因为这不符合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
的数据。我的图形现在显示得很好,但我不知道如何为每个图形定义独立的数据集。如何做到这一点?您需要检查图形的哪个实例正在调用数据源/委托。您可以通过将数据源/委托方法中包含的参数lineGraph
与图形实例进行比较来找到答案
我对swift不够熟悉,这里是如何在Objective-C中实现它的
- (CGFloat)lineGraph:(BEMSimpleLineGraphView *)graph valueForPointAtIndex:(NSInteger)index
{
if ([graph isEqual:self.myGraph1])
{
return data;
}
else
{
return data2;
}
}
谢谢你的回答:)不幸的是,我早就想出了这个办法。最后,我将第二个数据集保存在AppDelegate中,因为它是我知道如何查找的实例。感谢您抽出时间回复,祝您一切顺利@user3185748无法提供更详细的信息?你能解释一下你是如何解决这个问题的吗?